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.

Tracking changes<br />

And I should see "Open" within "#ticket .state"<br />

Then show me the page<br />

Then I should see "Open" within "#comments"<br />

When you run your scenario again, it should pop up a browser window that shows you<br />

the exact same page as Cucumber sees, shown in figure 10.13.<br />

Here you can see the partials rendered with their HTML shown on the page. This is<br />

because code coming from a string in a helper is automatically escaped, which is helpful—but<br />

sometimes things like this happen. Better safe than sorry!<br />

10.3.5 Automatic escaping saves your bacon<br />

This automatic escaping of strings is one of Rails 3’s big features. This saves your<br />

bacon because it stops malicious output being input into forms accidentally. This output<br />

would be things like your ticket’s description or comment text, which comes from<br />

the users. Never trust the users with outputting nonmalicious content!<br />

If this input wasn’t escaped before it was output to the page, it could potentially<br />

contain HTML tags like , which could contain malicious content. Rails 3 saves<br />

your bacon in this respect by automatically escaping all strings output to the view<br />

through ERB tags. Yes, sometimes it will be overzealous and escape things you don’t<br />

want it to escape, like your state partials.<br />

You can tell it that the string is safe by calling html_safe in your helper, changing<br />

it to this:<br />

"#{render comment.previous_state} &rarr; #{render comment.state}".html_safe<br />

The html_safe method effectively tells Rails, “It’s cool, man,” and Rails won’t escape<br />

anything inside the string. When you run bin/cucumber features/creating_tickets<br />

.feature:33, your scenario will now pass:<br />

1 scenario (1 passed)<br />

17 steps (17 passed)<br />

A browser window will appear, showing you the correct states, as shown in figure 10.14.<br />

Let’s remove the “Then show me the page” step from this scenario now, and you’re<br />

done. You’ve got your application showing the users what state a comment has<br />

switched the ticket to. Now’s a good time to check that you haven’t broken anything.<br />

When you run rake cucumber:ok spec, you should see that everything is A-OK:<br />

49 scenarios (49 passed)<br />

516 steps (516 passed)<br />

# and<br />

33 examples, 0 failures, 16 pending<br />

Figure 10.13 The states aren’t what they should be…<br />

267

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

Saved successfully!

Ooh no, something went wrong!