[LRUG] Testing email invitations with Cucumber

Graham Ashton graham at effectif.com
Tue Jan 29 11:15:32 PST 2013


On 29 Jan 2013, at 18:35, Ian Kynnersley <iankynnersley at gmail.com> wrote:

> We are using email_spec (https://github.com/bmabey/email-spec) to do the email interaction stuff and it seems to work well but the scenarios end up pretty tortuous.

I really don't think there's any need to test all that in one test, or with cucumber.

The first half of your steps are checking that an email gets sent to the right person. The second half just check that the email's subject is correct, and that it has a specific link in it. That sounds a lot like unit testing to me.

Why are you using cucumber? Are any non technical people on your team eager to read your tests?

This is how I do it:

- functional tests on my controller that checks an email gets sent

- functional tests on my mailer to check they contain the right links

Here's a gist with the very tests in:

https://gist.github.com/4666549

I've done it this way on a few apps, and have never had any trouble. (The "an" and "the" blocks are from nutrasuite; think of them as "describe".)

> Does this indicate the email should be sent from somewhere else (in the model or maybe a creator class) so that my "Given I have been invited" step will automatically generate the email.

I wouldn't decide where to send the email from based on whether or not I can persuade my testing framework to see it.

I'd think about when the email should be sent. For example, if you used an after_create callback on your model you wouldn't be able to create an invitation at the console without sending an email out. If you were doing some admin (e.g. fixing an invitation that didn't get delivered for some reason), that could cause a bunch of unwanted messages to get sent out without you realising.

Better to make it explicit, and control when the email is sent yourself.

You could make a method in the model that creates the invite and sends the email (which would be handy if you wanted to invite somebody from the Rails console), or you could just do it in your controller and manually call MyMailer.my_message(my_model).deliver to send an email from the console, should you need to.

What I'm trying to say is "there's nothing wrong with sending it in the controller".

> Has anyone done this? It is pretty standard behaviour. Does anyone have any recommendations?

[dons flame proof suit]

I'd suggest you probably don't need to write a full end to end integration test for it. How necessary it is will depend on your app. What are the failure modes? What will the integration test catch that a handful of unit tests wouldn't?

If you think "should send an email to invite new users" test in my gist is too lax (in the context of my app it isn't - there's only one email that could get sent), you could just tweak it so that it checks that the last email in ActionMailer::Base.deliveries is the correct message (e.g. by checking the subject).

--
Graham Ashton
Founder, The Agile Planner
http://www.theagileplanner.com | @agileplanner | @grahamashton







More information about the Chat mailing list