behavior driven development with rails

Post on 11-May-2015

1.773 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

Behavior Driven Developmentby Mark MenardVita Rara, Inc.

© 2004 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

© 2004 Vita Rara, Inc.

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.

© 2004 Vita Rara, Inc.

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));}

© 2004 Vita Rara, Inc.

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"); }}

© 2004 Vita Rara, Inc.

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.

© 2004 Vita Rara, Inc.

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.

© 2004 Vita Rara, Inc.

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.

© 2004 Vita Rara, Inc.

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."

© 2004 Vita Rara, Inc.

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.

© 2004 Vita Rara, Inc.

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

© 2004 Vita Rara, Inc.

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 ... endend

When you run this example, RSpec can provide a report like this:

Account when first created- should have a balance of $0

© 2004 Vita Rara, Inc.

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.

© 2004 Vita Rara, Inc.

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.

© 2004 Vita Rara, Inc.

The Example Application

A simple check book ledger.

top related