Behavior Driven Development with Rails


Published on

Introduction to behavior driven development and Ruby on Rail covering RSpec and Cucumber.

1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Show example_4_spec.rb
  • Behavior Driven Development with Rails

    1. 1. Behavior Driven Development by Mark Menard Vita Rara, Inc.
    2. 2. Test Driven Development <ul><li>TDD is a development process where tests for code are written before the writing of implementation code. </li></ul><ul><li>Has been primarily focused on unit testing. </li></ul><ul><ul><li>In recent years functional and integration testing have become more common. </li></ul></ul><ul><li>Some examples of unit testing tools: </li></ul><ul><ul><li>Java: JUnit </li></ul></ul><ul><ul><li>C#: NUnit </li></ul></ul><ul><ul><li>Ruby: Test::Unit </li></ul></ul><ul><ul><li>Many languages have xUnit frameworks </li></ul></ul>
    3. 3. xUnit Testing <ul><li>xUnit frameworks focus on creating tests for code. </li></ul><ul><ul><li>There is little opportunity to involve the consumers of the software in the process. </li></ul></ul><ul><ul><li>The language and style speak directly to software developers, not stake holders. </li></ul></ul><ul><ul><li>Is very code centric </li></ul></ul><ul><li>Tests are typically organized in classes. </li></ul><ul><li>Very few domain experts can read xUnit tests. </li></ul>
    4. 4. JUnit Test Example @ Test public void simpleAdd() { Money m12CHF = new Money ( 12 , &quot;CHF&quot; ); Money m14CHF = new Money ( 14 , &quot;CHF&quot; ); Money expected = new Money ( 26 , &quot;CHF&quot; ); Money result = m12CHF . add(m14CHF); assertTrue(expected . equals(result)); }
    5. 5. JUnit Test Fixture Setup public class MoneyTest { private Money f12CHF; private Money f14CHF; private Money f28USD; @Before public void setUp () { f12CHF = new Money ( 12 , &quot;CHF&quot; ); f14CHF = new Money ( 14 , &quot;CHF&quot; ); f28USD = new Money ( 28 , &quot;USD&quot; ); } }
    6. 6. Behavior Driven Development <ul><li>Focus on what something should do. </li></ul><ul><li>Encourages a literate style. </li></ul><ul><li>Supports extraction of the specification in a readable format. </li></ul><ul><li>Specs should be readable by domain experts. </li></ul><ul><li>Specs should be executable. </li></ul><ul><li>Should support testing code in isolation, integrated and full stack. </li></ul><ul><li>When the spec runs you’re done. </li></ul>
    7. 7. My BDD Tool Kit <ul><li>Cucumber </li></ul><ul><ul><li>Specify what the user should see and be able to do. </li></ul></ul><ul><li>RSpec </li></ul><ul><ul><li>Specify what code should do. </li></ul></ul><ul><li>Mocha </li></ul><ul><ul><li>Mocking and stubbing library. </li></ul></ul><ul><li>FactoryGirl </li></ul><ul><ul><li>Automated setup of database fixtures. </li></ul></ul><ul><ul><li>Integrates with Rails ActiveRecord </li></ul></ul><ul><ul><li>Implementation of the Fixture Factory pattern. </li></ul></ul>
    8. 8. Cucumber <ul><li>Cucumber lets software development teams describe how software should behave in plain text. </li></ul><ul><li>Cucumber features are written in a business-readable domain-specific language and serve as: </li></ul><ul><ul><li>Documentation </li></ul></ul><ul><ul><li>Automated tests </li></ul></ul><ul><ul><li>Development aid </li></ul></ul><ul><li>Cucumber works with Ruby, Java, .Net, Flex, or web apps in any language. </li></ul><ul><li>It has been translated into over 30 spoken languages, including lolz. </li></ul>
    9. 9. Cucumber Example Feature: Contact Us As an anonymous user I want to be able to send a message So that I can receive support or get an answer to a question Scenario: Load Form Given I am on &quot;home page&quot; When I follow &quot;Contact Us&quot; Then I should see &quot;Please use the following form&quot; Scenario: Submitting form Given I am on the &quot;contact us page&quot; When I fill in &quot;contact_handler[email]&quot; with &quot;; And I fill in &quot;contact_handler[subject]&quot; with &quot;Help&quot; And I fill in &quot;contact_handler[body]&quot; with &quot;Content&quot; And I press &quot;send&quot; Then I should see &quot;Thanks for your message.&quot;
    10. 10. Why Cucumber? <ul><li>Your clients can help you create the feature specifications. </li></ul><ul><li>When it’s green you’re done. </li></ul><ul><ul><li>Stop the dithering. </li></ul></ul><ul><ul><li>You have an agreed on deliverable. </li></ul></ul><ul><ul><li>If the deliverable request changes then the spec changes. Makes clients aware they’re moving the goal post. </li></ul></ul><ul><li>In Rails it is very easy to test the complete app stack. </li></ul><ul><li>Can be used with any web application. </li></ul><ul><ul><li>.Net, ASP, Java, PHP, Python. Django, etc. </li></ul></ul>
    11. 11. RSpec <ul><li>RSpec provides a domain specific language with which you can express executable examples of the expected behavior of your code. </li></ul><ul><li>RSpec is code centric. </li></ul><ul><li>RSpec includes support for mocking and stubbing. </li></ul><ul><li>I use it as a replacement for unit and integration testing. </li></ul><ul><ul><li>Models and controllers </li></ul></ul>
    12. 12. An RSpec Example You: Describe an account when it is first created. Customer: It should have a balance of $0. describe Account, &quot;when first created&quot; do it &quot;should have a balance of $0&quot; do ... end end When you run this example, RSpec can provide a report like this: Account when first created - should have a balance of $0
    13. 13. Why RSpec? <ul><li>Extremely expressive </li></ul><ul><li>Supports building of test contexts in steps. </li></ul><ul><li>Built in support for mocking and stubbing to support testing in isolation. </li></ul><ul><ul><li>I personally use Mocha, but starting with RSpec is fine. They both work. </li></ul></ul><ul><li>Easily produce specifications for client approval. </li></ul><ul><li>Has a literate style accessible to domain experts. </li></ul><ul><li>Has excellent support for Rails. </li></ul>
    14. 14. Mocha <ul><li>A mocking and stubbing library for Ruby. </li></ul><ul><li>Uses a syntax similar to JMock. </li></ul><ul><li>Mocha provides a unified, simple and readable syntax for both traditional mocking and partial mocking. </li></ul><ul><li>Commonly used to isolate layers in an application to isolate tests. </li></ul><ul><li>Can also be used to test expected collaborations between classes. </li></ul>
    15. 15. The Example Application <ul><li>A simple check book ledger. </li></ul>