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.

Sending ticket notifications<br />

Then they should see "updated the Release date ticket" in the email body<br />

And the email should contain 2 parts<br />

And there should be a part with content type "text/plain"<br />

And there should be a part with content type "text/html"<br />

When you run this feature with bin/cucumber features/ticket_notifications<br />

.feature, you’re notified that the two steps you’ve just used are undefined:<br />

Then /^the email should contain two parts$/ do<br />

pending # express the regexp above with the code you wish you had<br />

end<br />

Then /^there should be a part with content type "([^"]*)"$/ do | arg1 |<br />

pending # express the regexp above with the code you wish you had<br />

end<br />

The email_spec gem doesn’t provide any steps for this, so you must craft your own.<br />

The gem does provide some helper methods that you can use. You should define<br />

these steps in a file separate from features/step_definitions/email_steps.rb, because the<br />

next time you run the email_spec generator, it will overwrite this file. Let’s instead<br />

put them in features/step_definitions/app_email_steps.rb and define them like this:<br />

Then /^the email should contain (\d+) parts$/ do |num|<br />

current_email.parts.size.should eql(num.to_i)<br />

end<br />

Then /^there should be a part with content type "([^"]*)"$/<br />

do |content_type |<br />

current_email.parts.detect do |p|<br />

p.content_type == content_type<br />

end.should_not be_nil<br />

end<br />

In the first step here, the current_email method comes from email_spec and represents<br />

the currently opened email. You open this email with the “Then alice@ticketee<br />

.com opens the email” step in your scenario. This object is a Mail::Message object,<br />

which represents an email object. You check in this step that the email contains the<br />

number of parts you say it should contain, and convert the num variable to an integer<br />

using to_i because it comes in from the step definition as a String object.<br />

In the second step, you iterate through the parts to the email, using detect to<br />

return the first part in parts, which matches the condition inside the block you specify.<br />

You don’t care at this stage what order the parts appear in (that’s something the different<br />

email clients will deal with), but you do care that there’s more than one part.<br />

When you run your feature using bin/cucumber features/ticket_notifications<br />

.feature, you see that the first of your two newest steps fails:<br />

And the email should contain 2 parts<br />

expected 2<br />

got 0<br />

So your scenario expected to see two parts, but got none. Why not even one? Well, the<br />

normal flavor of emails don’t come with multiple parts, because the text is part of the<br />

323

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

Saved successfully!

Ooh no, something went wrong!