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.

84 CHAPTER 4 Oh CRUD!<br />

4.1.1 Writing a feature<br />

Create a new file in the features directory called viewing_projects.feature, shown in the<br />

following listing.<br />

Listing 4.1 features/viewing_projects.feature<br />

Feature: Viewing projects<br />

In order to assign tickets to a project<br />

As a user<br />

I want to be able to see a list of available projects<br />

Scenario: Listing all projects<br />

Given there is a project called "TextMate 2"<br />

And I am on the homepage<br />

When I follow "TextMate 2"<br />

Then I should be on the project page for "TextMate 2"<br />

If you run rake cucumber:ok here, all the features will run. Instead, you want to run<br />

just the feature you’re working on because you don’t want the other feature to be<br />

altered by what you do here. When you’re done, you’ll run rake cucumber:ok to<br />

ensure that everything is still working.<br />

To run just this one feature, use the bin/cucumber executable, which was added to<br />

your project when you ran bundle install --binstubs. If you didn’t use the<br />

--binstubs option, you would have to use bin/cucumber instead, and typing all of<br />

that gets a bit boring after a while.<br />

Now, to run this single feature, you run the following command:<br />

bin/cucumber features/viewing_projects.feature<br />

You should always use bin/cucumber rather than straight cucumber to run the bundled<br />

version of your gems because you could be using different versions of the same<br />

gem across separate projects. By running the bin/cucumber command, you ensure<br />

that you’re loading the version of the gem specified by your bundle rather than the<br />

system version of that gem.<br />

The first line of the only scenario in the feature fails, all because you haven’t<br />

defined the “there is a project” step yet, as Cucumber informs you in the output:<br />

3 scenarios (1 undefined, 2 passed)<br />

15 steps (3 skipped, 1 undefined, 11 passed)<br />

You can implement step definitions for<br />

undefined steps with these snippets:<br />

Given /^there is a project called "([^"]*)"$/ do |arg1|<br />

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

end<br />

As you can see in the step output, you’ve got one undefined step. Underneath that,<br />

Cucumber very handily gives you a step definition you can use to define this step. In this<br />

step, you need to create a new Project object. Rather than doing it manually by calling<br />

Project.create everywhere you need it, you can set up a little thing called factories.

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

Saved successfully!

Ooh no, something went wrong!