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.

74 CHAPTER 3 Developing a real Rails application<br />

method_missing. These are then used to construct a real method call and make Ruby<br />

act as though the method exists. When you use the bang (find_by_name! with an<br />

exclamation mark as opposed to find_by_name) version of this method, Active<br />

Record raises an ActiveRecord::RecordNotFound exception if the record isn’t found.<br />

This can prove helpful if, for example, you misspell a project’s name when trying to<br />

use this step. The exception raised if you don’t capitalize the M in TextMate (thereby<br />

making it Textmate) is this:<br />

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

Couldn't find Project with name = Textmate 2 (ActiveRecord::RecordNotFound)<br />

If you don’t use the bang version of this method, the finder returns nil. If<br />

project_path is passed nil, you get a hard-to-debug error:<br />

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

No route matches {:action => "show", :controller => "projects"}<br />

This error claims that no route matches up to the ProjectsController’s show action,<br />

but there actually is one: /projects/:id. The difference is that this route requires<br />

you to pass through the id parameter too; otherwise the error occurs.<br />

To debug something like this, you check and double check what values were being<br />

passed where, particularly what $1 was coming back as, and whether Project.find_by_name<br />

was returning any record. If you checked those two things, you’d<br />

find that Project.find_by_name isn’t returning what you think it should be returning,<br />

and hopefully you’d realize you were passing in the wrong name.<br />

Upon running rake cucumber:ok, you now see that a step passes, but the rest of<br />

the feature fails:<br />

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

And I should see "TextMate 2 - Projects - Ticketee"<br />

expected #has_content?("TextMate 2 - Projects - Ticketee")<br />

to return true, got false<br />

Why are you getting an error seemingly from RSpec? Because Capybara uses its helpers<br />

internally to determine if it can find content. This error therefore means that Capybara<br />

can’t find the content it’s looking for. What content? Have a look at the last line<br />

of the backtrace:<br />

features/creating_projects.feature:13:<br />

Line 13 of features/creating_projects.feature is the And I should see "TextMate 2 -<br />

Projects - Ticketee" step, which checks for content shown on the page. To make<br />

this step pass, you need to define the content it should see. Write the code from the<br />

following listing into app/views/projects/show.html.erb.<br />

Listing 3.22 app/views/projects/show.html.erb<br />

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

Saved successfully!

Ooh no, something went wrong!