27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Behavior-driven development<br />

all the steps as Givens, but it’s not really readable. Let’s now implement this example<br />

scenario in Cucumber. First, run mkdir -p accounts/features, which, much as in the<br />

RSpec example, creates a directory called accounts and a directory inside of that<br />

called features. In this features directory, create a file called account.feature. In this file,<br />

you write a feature definition, as shown in the following listing.<br />

Listing 2.21 accounts/features/account.feature<br />

Feature: My Account<br />

In order to manage my account<br />

As a money minder<br />

I want to ensure my money doesn't get lost<br />

This listing lays out what this feature is about and is more useful to human readers<br />

(such as stakeholders) than it is to Cucumber.<br />

Next, you put in your scenario underneath the feature definition, as in the following<br />

listing.<br />

Listing 2.22 accounts/features/account.feature<br />

Scenario: Taking out money<br />

Given I have an account<br />

And it has a balance of 100<br />

When I take out 10<br />

Then my balance should be 90<br />

The whole feature should now look like the following listing.<br />

Listing 2.23 accounts/features/account.feature<br />

Feature: My Account<br />

In order to manage my account<br />

As a money minder<br />

I want to ensure my money doesn't get lost<br />

Scenario: Taking out money<br />

Given I have an account<br />

And it has a balance of 100<br />

When I take out 10<br />

Then my balance should be 90<br />

As you can see in listing 2.23, it’s testing the whole stack of the transaction rather than<br />

a single unit. This process is called integration testing. You set the stage by using the Given<br />

steps, play out the scenario using When steps, and ensure that the outcome is as you<br />

expected by using Then steps. The And word is used when you want a step to be defined<br />

in the same way as the previous step, as seen in the first two lines of this scenario.<br />

To run this file, you first need to ensure Cucumber is installed, which you can do<br />

by installing the Cucumber gem: gem install cucumber. When the Cucumber gem is<br />

installed, you can run this feature file by going into the accounts directory and running<br />

cucumber features, as in the next listing.<br />

37

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!