27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

38 CHAPTER 2 Testing saves your bacon<br />

Listing 2.24 Terminal<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 />

This output appears in color in the terminal with the steps of the scenario in yellow, 7<br />

followed by a summary of this Cucumber run, and then a list of what code you used to<br />

define the missing steps (not shown in this example output), again in yellow. What<br />

this output doesn’t tell you is where to put the step definitions. Luckily, this book does.<br />

All these step definitions should go into a new file located at features/step_definitions/<br />

account_steps.rb. The file is called account_steps.rb and not account.rb to clearly separate<br />

it from any other Ruby files, so when looking for the steps file, you don’t get it<br />

confused with any other file. In this file, you can copy and paste in the steps Cucumber<br />

gave you, as in the following listing.<br />

Listing 2.25 features/step_definitions/account_steps.rb<br />

Given /^I have an account$/ do<br />

pending # express the regexp above with the code you wish you had<br />

end<br />

Given /^it has a balance of (\d+)$/ do |arg1|<br />

pending # express the regexp above with the code you wish you had<br />

end<br />

When /^I take out (\d+)$/ do |arg1|<br />

pending # express the regexp above with the code you wish you had<br />

end<br />

Then /^my balance should be (\d+)$/ do |arg1|<br />

pending # express the regexp above with the code you wish you had<br />

end<br />

If you run cucumber features again, you’ll see that all your steps are defined but not<br />

run (signaled by their blue coloring) except the very first one, which is yellow because<br />

it’s pending. Now you’re going to restructure the first step to make it not pending. It<br />

will now instantiate an Account object that you’ll use for the rest of this scenario.<br />

7 If you’re running Windows, you may need to install ANSICON in order to get the colored output. This process<br />

is described at https://github.com/adoxa/ansicon, and you can download ANSICON from http://adoxa<br />

.110mb.com/ansicon/.

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

Saved successfully!

Ooh no, something went wrong!