Behavior Driven Development with Rails

  • 1,682 views
Uploaded on

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

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

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
1,682
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
34
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • Show example_4_spec.rb

Transcript

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