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.

334 CHAPTER 12 Sending email<br />

application through rails server. But how do you ensure that it works all the time?<br />

The feature will provide that insurance.<br />

When you’re done here, you’ll have your application hooked up to Gmail’s SMTP<br />

server so that you can send emails in the real world, and you’ll have a Cucumber feature<br />

to ensure that it’s never broken. 9 Let’s jump into it.<br />

12.3.1 Testing real-world email<br />

In this section, you’ll create a feature in which you set up Action Mailer to send out<br />

emails to Gmail’s SMTP service. You’ll then update a ticket’s comment, which should<br />

trigger the emails to be sent to the real world. Finally, you’ll check the Gmail account<br />

(using the mail gem on which Action Mailer is based), to make sure that the email<br />

was received. If it wasn’t received, then the cause is most likely a configuration problem,<br />

such as an invalid password.<br />

Let’s write this new feature in a new file called features/gmail.feature a bit at a time.<br />

You start with just these few lines:<br />

Feature: Gmail<br />

In order to send real world emails<br />

As the application<br />

I want to ensure my configuration is correct<br />

Background:<br />

Given there are the following users:<br />

| email | password |<br />

| alice@ticketee.com | password |<br />

| youraccount@example.com | password |<br />

And Action Mailer delivers via SMTP<br />

Here you have the standard feature setup on the first four lines, describing to any<br />

interested reader what this feature’s purpose is. The final few lines begin your feature’s<br />

Background, defining two users that you’ll use and an as-of-yet undefined step.<br />

The two users set up here are for setting up the ticket a little later on in your Background.<br />

The youraccount@example.com should be your actual Gmail account’s<br />

address 10 for reasons that you’ll see later. 11<br />

The current undefined step on the final line will be used to tell Action Mailer not<br />

to capture all emails that are being sent, but rather to send them out through the<br />

SMTP configuration that you’ll set up in a little while. When you run this feature using<br />

bin/cucumber features/gmail.feature, you’re given the step definition for this step:<br />

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

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

end<br />

9 That is to say, if you run all the tests and they all pass before you commit, then you know that your Gmail connection<br />

would be working, too.<br />

10 And if you don’t have one, sign up! It’s free and will only take a minute.<br />

11 We’d use youraccount@gmail.com as the example here, but we don’t want to bombard the owner of this<br />

actual account with emails!

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

Saved successfully!

Ooh no, something went wrong!