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.

Real-world email<br />

In this step definition, you need to tell Action Mailer to use SMTP to deliver your<br />

emails rather than capture them. You can define this new step definition in features/<br />

step_definitions/app_email_steps.rb like this:<br />

Given /^Action Mailer delivers via SMTP$/ do<br />

ActionMailer::Base.delivery_method = :smtp<br />

end<br />

Great! Now Action Mailer will set the delivery_method to :smtp before every scenario<br />

in this feature. The side effect of this setting is that it will be set for every scenario that<br />

runs after it, not only scenarios in this feature. This is a problem because you don’t<br />

want every scenario to send email in the real world, only the ones contained in this<br />

feature. To make it revert this setting back to :test after every scenario, you can create<br />

a new file at features/support/after_hook.rb and put this content inside it:<br />

After do<br />

ActionMailer::Base.delivery_method = :test<br />

end<br />

The After method here is provided by Cucumber, and its purpose is to execute any<br />

given block after each scenario has finished running. With this code, the setting will<br />

be reverted back to :test and you’ll only be sending real-world emails in this particular<br />

scenario.<br />

Let’s continue writing your new feature. You need to set up a project that both<br />

alice@ticketee.com and you can see, and create a ticket on that project that is posted<br />

by you. In a short while, you’ll get Alice to sign in and post a comment to this ticket,<br />

which should make an email appear in your inbox. You’ll then check this email using<br />

the mail gem. Set up the project and ticket with these steps:<br />

Given there is a project called "TextMate 2"<br />

And "alice@ticketee.com" can view the "TextMate 2" project<br />

And "youraccount@example.com" can view the "TextMate 2" project<br />

And "youraccount@example.com" has created a ticket for this project:<br />

| title | description |<br />

| Release date | TBA very shortly. |<br />

Here’s another place where youraccount@example.com should be substituted with<br />

your real Gmail account. In these steps, you set up that alice@ticketee.com and your<br />

email both have the “view” permission on the TextMate 2 project. After this, you need<br />

a ticket that you’ve created so that Alice can post a comment to it and you can receive<br />

an email notification informing you of what Alice has posted.<br />

Now you can get to the meat of your feature: the scenario itself. In this scenario,<br />

you want to log in as alice@ticketee.com, visit the “Release date” ticket inside the Text-<br />

Mate 2 project, and post a comment to it. After all that’s said and done, you need to<br />

assert that your youraccount@example.com mailbox has one new message. The code<br />

for the scenario should therefore look like in the following listing.<br />

335

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

Saved successfully!

Ooh no, something went wrong!