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.

36 CHAPTER 2 Testing saves your bacon<br />

Cucumber’s history is intertwined with RSpec, so the two are similar. In the beginning<br />

of BDD, as you know, there was RSpec. Shortly thereafter, there were RSpec Stories,<br />

which looked like the following listing.<br />

Listing 2.19 Example<br />

Scenario "savings account is in credit" do<br />

Given "my savings account balance is", 100 do |balance|<br />

@account = Account.new(balance)<br />

end<br />

...<br />

end<br />

The idea behind RSpec Stories is that they are code- and human-readable stories that<br />

can be used for automated testing as well as quality assurance (QA) testing by stakeholders.<br />

Aslak Hellesoy rewrote RSpec Stories during October 2008 into what we<br />

know today as Cucumber. The syntax remains similar, as seen in the following listing.<br />

Listing 2.20 Example<br />

Scenario: Taking out money<br />

Given I have an account<br />

And my account balance is 100<br />

When I take out 10<br />

Then my account balance should be 90<br />

What you see here is known as a scenario in Cucumber. Under the scenario’s title, the<br />

remainder of the lines are called steps, which are read by Cucumber’s parser and<br />

matched to step definitions, where the logic is kept. Scenarios are found inside a feature,<br />

which is a collection of common scenarios. For example, you may have one feature for<br />

dictating what happens when a user creates projects and another for when a user creates<br />

tickets.<br />

Notice the keywords Given, And, When, and Then. These are just some of the keywords<br />

that Cucumber looks for to indicate that a line is a step. If you’re going to be<br />

using the same keyword on a new line, it’s best practice to instead use the And keyword<br />

because it reads better. Try reading the first two lines aloud from the previous listing,<br />

then replace the And with Given and try it again. It just sounds right to have an And<br />

there rather than a Given.<br />

Given steps are used for setting up the scene for the scenario. This example sets up<br />

an account and gives it a balance of 100.<br />

When steps are used for defining actions that should happen during the scenario.<br />

The example says, When I take out 10.<br />

Then steps are used for declaring things that take place after the When steps have<br />

completed. This example says, When I take out 10 Then my account balance should<br />

be 90.<br />

These different step types add a great deal of human readability to the scenarios in<br />

which they’re used, even though they can be used interchangeably. You could define

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

Saved successfully!

Ooh no, something went wrong!