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.

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

@account = Account.new<br />

end<br />

Behavior-driven development<br />

Steps are defined by using regular expressions, which are used when you wish to match<br />

strings. In this case, you’re matching the step in the feature with this step definition by<br />

putting the text after the Given keyword into a regular expression. After the regular<br />

expression is the do Ruby keyword, which matches up with the end at the end. This<br />

syntax indicates a block, and this block is run (“called”) when this step definition is<br />

matched.<br />

With this step defined, you can try running cucumber features/account.feature<br />

to see if the feature will pass. No—it fails with this error:<br />

Given I have an account<br />

uninitialized constant Object::Account (NameError)<br />

Similar to the beginning of the RSpec showcase, create a lib directory inside your<br />

accounts directory. To define this constant, you create a file in this new directory<br />

called account.rb. In this file you put code to define the class, shown in the following<br />

listing.<br />

class Account<br />

end<br />

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

Listing 2.27 accounts/lib/account.rb<br />

This file is not loaded automatically, of course: you have to require it just as you did in<br />

RSpec with lib/bacon.rb. Cucumber’s authors already thought of this and came up<br />

with a solution. Any file inside of features/support is automatically loaded, with one<br />

special file being loaded before all the others: features/support/env.rb. This file<br />

should be responsible for setting up the foundations for your tests. Now create features/support/env.rb<br />

and put these lines inside it:<br />

$LOAD_PATH

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

Saved successfully!

Ooh no, something went wrong!