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.

86 CHAPTER 4 Oh CRUD!<br />

Aha! You’re now told there’s no such factory! Then you’d better get around to creating<br />

one. You use these factories not only in your Cucumber features but also later in<br />

your RSpec tests. Placing the factories inside the features directory isn’t fair to the<br />

RSpec tests, and placing them inside the spec isn’t fair to the Cucumber tests. So<br />

where do they go? Create a new folder at the root of your application for them, and<br />

name it factories. Inside this directory, create your Project factory by creating a new<br />

file called project_factory.rb and filling it with the following content:<br />

Factory.define :project do |project|<br />

project.name 'Ticketee'<br />

end<br />

This small snippet defines your project factory, which creates a new instance of the<br />

Project class, defaulting the name attribute to Ticketee. These files aren’t going to<br />

load themselves, so you must create a new file at features/support/factories.rb and put<br />

this content in it:<br />

Dir[Rails.root + "factories/*.rb"].each do |file|<br />

require file<br />

end<br />

All .rb files in features/support are loaded automatically before Cucumber starts, so all<br />

the files in the factories are required, and they define all the factories in the files.<br />

With this factory defined, your feature should have nothing to whine about. Let’s<br />

look at the following listing to see what happens now when you run bin/cucumber<br />

features/viewing_projects.feature<br />

Listing 4.2 features/viewing_projects.feature failure<br />

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

And I am on the homepage<br />

When I follow "TextMate 2"<br />

no link with title, id or text 'TextMate 2' found ...<br />

A link appears to be missing. You’ll add that right now.<br />

4.1.3 Adding a link to a project<br />

Capybara is expecting a link on the page with the words “TextMate 2” but can’t find it.<br />

The page in question is the homepage, which is the index action from your<br />

ProjectsController. Capybara can’t find it because you haven’t yet put it there,<br />

which is what you’re going to do now. Open app/views/projects/index.html.erb, and<br />

add the contents of the following listing underneath the first link.<br />

Listing 4.3 app/views/projects/index.html.erb<br />

Projects<br />

<br />

<br />

<br />

<br />

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

Saved successfully!

Ooh no, something went wrong!