Behavior Driven Development with Rails
Upcoming SlideShare
Loading in...5
×
 

Behavior Driven Development with Rails

on

  • 2,208 views

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.

Statistics

Views

Total Views
2,208
Views on SlideShare
2,205
Embed Views
3

Actions

Likes
1
Downloads
34
Comments
0

1 Embed 3

http://www.slideshare.net 3

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • Show example_4_spec.rb

Behavior Driven Development with Rails Behavior Driven Development with Rails Presentation Transcript

  • Behavior Driven Development by Mark Menard Vita Rara, Inc.
  • 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
  • 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.
  • 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)); }
  • 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" ); } }
  • 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.
  • 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.
  • 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.
  • 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."
  • 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.
  • 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
  • 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
  • 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.
  • 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.
  • The Example Application
    • A simple check book ledger.