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.

Linking tickets to users<br />

Make sure to run db:test:prepare<br />

If you don’t prepare the test database, the following error occurs when you run the<br />

feature:<br />

And I press "Create Ticket"<br />

undefined method 'email' for nil:NilClass (ActionView::Template::Error)<br />

Watch out for that one.<br />

Only one scenario fails now. We’re right back to the missing “Created by user@ticketee<br />

.com” text. Open app/views/tickets/show.html.erb and, above the ticket description,<br />

put the following line:<br />

Created by <br />

This line adds the text the feature needs to pass, so when you run bin/cucumber<br />

features/creating_tickets.feature, you get the following output:<br />

3 scenarios (3 passed)<br />

44 steps (44 passed)<br />

You should run rake cucumber:ok spec as usual to ensure you haven’t broken anything:<br />

Failing Scenarios:<br />

cucumber features/deleting_tickets.feature:15<br />

cucumber features/editing_tickets.feature:16<br />

cucumber features/viewing_tickets.feature:18<br />

Oops, it looks like you did! If you didn’t have these tests in place, you wouldn’t have<br />

known about this breakage unless you tested the application manually or guessed (or<br />

somehow knew) that your changes would break the application in this way. Let’s see if<br />

you can fix it.<br />

6.5.2 We broke something!<br />

Luckily, all the failed tests have the same error<br />

When I follow "Make it shiny!"<br />

undefined method 'email' for nil:NilClass (ActionView::Template::Error)<br />

...<br />

./app/views/tickets/show.html.erb:4:in ...<br />

Whatever is causing this error is on line 4 of app/views/tickets/show.html.erb:<br />

Created by <br />

Aha! The error is undefined method 'email' for nil:NilClass, and the only place<br />

you call email on this line is on the user object from @ticket, so you can determine<br />

that user must be nil. But why? Let’s have a look at how to set up the data in the<br />

features/viewing_tickets.feature feature, as shown in the following listing.<br />

131

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

Saved successfully!

Ooh no, something went wrong!