[LRUG] Testing email invitations with Cucumber
Ian Kynnersley
iankynnersley at gmail.com
Tue Jan 29 11:47:11 PST 2013
Hi Graham,
Thanks for your very helpful response. I think you're right that testing
the email contents is a job for a unit test.
I would still like to have cucumber coverage for the "invited user clicks
on the link in the email". However, I guess we could validate that the link
in the email is correct in the unit test and then have a cucumber feature
that says:
Given I have been invited
When I visit the "accept invitation" page
Then I should see ....
I also agree that for this app, the controller is probably the right place
for these emails to be triggered (although better would be to wrap them in
some sort of invitation class).
And thanks for the Gist. It clarifies it nicely.
Cheers
Ian
On Tue, Jan 29, 2013 at 7:15 PM, Graham Ashton <graham at effectif.com> wrote:
> 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
>
>
>
>
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
--
Ian Kynnersley
http://iankynnersley.co.uk | +44 (0) 7973 420 829 |
http://twitter.com/kpopper
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20130129/cb427e1c/attachment-0003.html>
More information about the Chat
mailing list