[rspec-users] Side Effects in Stories

Ben Mabey ben at benmabey.com
Fri Oct 19 15:30:22 EDT 2007


Yeah, you have two options really.  The first is to do what linoj said 
and mock out the mailer that you are using.  The other way, which Pat, 
was referring to was to actually check the mail queue.  I generally do 
both.  The reason being is that I want to make sure that I am using the 
correct mailer, AND that when I do use the correct mailer it actually 
renders the mail.  So the latter option is more like doing controller 
testing with integration because the actual view for the email will be 
rendered based on what is passed into the mailer.  I do this because I 
don't usually spec out my mailers alone.   I add the following to my 
spec_helper to make testing mail a little nicer:

    def mailer_setup
      ActionMailer::Base.delivery_method = :test
      ActionMailer::Base.perform_deliveries = true
    end


    def empty_the_mailer!
      ActionMailer::Base.deliveries=[]
    end

    def mailer_should_deliver(ammount_to_send=1)
      lambda { yield}.should change(ActionMailer::Base.deliveries, 
:size).by(ammount_to_send)
    end

    def last_email_sent
     ActionMailer::Base.deliveries.last
    end

So in a spec you can say

  mailer_should_deliver 2 do
      @user.create!
   end

or you can say stuff like

last_email_sent.to.should == ....

etc...

-Ben




Jonathan Linowes wrote:
>
> On Oct 19, 2007, at 1:54 PM, s.ross wrote:
>
>> On Oct 18, 2007, at 6:09 AM, Daniel N wrote:
>>
>>> On 9/11/07, *s.ross* <cwdinfo at gmail.com <mailto:cwdinfo at gmail.com>> 
>>> wrote:
>>>
>>>     I have a story where the user resets the password [hey, this story
>>>     thing really rocks!]. It is expected that the password will change
>>>     and that the user will be redirected to a login screen. A side
>>>     effect
>>>     is that the user will receive email with his/her new password.
>>>
>>>     Where I'm stuck is in ascertaining whether mail was generated.
>>>
>>>     Any thoughts on how this might be accomplished?
>>>
>>>     Thanks
>>>
>>>     -----------------
>>>
>>>     Story: Reset the Password
>>>
>>>        As a registered user
>>>        I want to be able to reset the password and have it emailed to me
>>>        And then I want to log in
>>>
>>>
>>>     Scenario: Reset password for an account
>>>
>>>        Given A valid account is given:   joe at schmoe.com
>>>     <mailto:joe at schmoe.com>
>>>
>>>        When Resetting password for joe at schmoe.com
>>>     <mailto:joe at schmoe.com>
>>>
>>>        Then Password state should change in database joe at schmoe.com
>>>     <mailto:joe at schmoe.com>
>>>        And Registered user should be redirected to login screen
>>>        And Email should be sent to the registered user
>>>        And the new password should work :)
>>>     _______________________________________________
>>>
>>>
>>> Hi s.ross
>>>
>>> I know this was a while ago that you asked this question, but did  
>>> you end up coming up with a solution?
>>>
>>> -Daniel
>>
>> Nope, and embarrassingly, I took a different direction without 
>> preserving any traces of what I had been doing (can you say "frequent 
>> checkins?"). Ooops. Everything but verifying that the email was sent 
>> was running, but I never figured out how to verify that.
>>
>> --steve
>>
>>
>>
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org <mailto:rspec-users at rubyforge.org>
>> http://rubyforge.org/mailman/listinfo/rspec-users
>
> from my specs on restful_authentication's models/user_observer_spec.rb
>
> context "A UserObserver" do
>   setup do
>     @user = mock('user')
>     @user_observer = UserObserver.instance
>   end
>
>   specify "should call UserNotifier.deliver_signup_notification on 
> user creation" do
>     UserNotifier.should_receive(:deliver_signup_notification).with(@user)
>     @user.stub!(:pending?).and_return(true)
>     @user.stub!(:recently_activated?).and_return(false)
>     @user.stub!(:recently_forgot_password?).and_return(false)
>     @user_observer.after_save(@user)
>   end
>
> (sorry its in pre-1.0 format)
>
> of course, this checks whether the email gets sent, not whether it got 
> delivered... :)
>
> --linoj
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users



More information about the rspec-users mailing list