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.

def to_s<br />

name<br />

end<br />

Changing a ticket’s state<br />

By default, objects in Ruby have a to_s method that outputs the ugly version, the<br />

inspected version of this object, you saw earlier. By overriding this in the model to call<br />

the name method, you’ll get it to display the state’s name rather than its object output.<br />

When you refresh the page in your browser, you should see the<br />

correct state, as shown in figure 10.9.<br />

Great! This should mean that the last scenario in your “Creat-<br />

ing comments” feature will pass. Let’s run it with bin/cucumber<br />

features/creating_comments.feature and find out:<br />

1 scenario (1 passed)<br />

14 steps (14 passed)<br />

Indeed it’s passing! This is a good stage to ensure that everything is working by running<br />

rake cucumber:ok spec. Blast, one of the features is failing:<br />

Failing Scenarios:<br />

cucumber features/<br />

creating_comments.feature:26 # Scenario: Creating an invalid comment<br />

A broken feature often means a broken part of your code, so you should investigate<br />

this before continuing. If there are thoughts of “it’s only one feature,” think again. At<br />

what point do you draw the line? One? Two? Three failing scenarios? Let’s have a zerotolerance<br />

policy on these and fix them when they break.<br />

10.2.5 Fixing creating comments<br />

The entire reason why you write features before you write code is so that you can catch<br />

scenarios like this where something unexpectedly breaks. If you didn’t have these scenarios<br />

in place, then you wouldn’t be made aware of these scenarios until a user of<br />

your site stumbled across it. This isn’t what you want. You want your users to assume<br />

that you’re perfect.<br />

You should look into why this feature is failing and fix it right away. This particular<br />

scenario is failing with this backtrace:<br />

And I press "Create Comment"<br />

You have a nil object when you didn't expect it!<br />

You might have expected an instance of Array.<br />

The error occurred while evaluating nil.map (ActionView::Template::Error)<br />

./app/views/comments/_form.html.erb:12<br />

Here it claims you’re calling map on a nil object, and that it’s on line 12 of app/views/<br />

comments/_form.html.erb. The line it’s referencing is the following:<br />

<br />

Figure 10.9<br />

The correct state<br />

Alright, the only place where map is being called is on the @states variable, so it’s<br />

pretty straightforward that @states is the nil object. But how did it come to be? Let’s<br />

review this scenario, as shown in the following listing.<br />

261

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

Saved successfully!

Ooh no, something went wrong!