[rspec-users] Spec'ing ActionMailer

David Chelimsky dchelimsky at gmail.com
Mon Feb 5 14:12:25 EST 2007


On 2/5/07, s.ross <cwdinfo at gmail.com> wrote:
> David--
>
> I still don't have this working (see my previous email with the
> pastie attached), but now have another ActionMailer/rSpec question.
> I'm spec'ing a different mailer model directly (instead of through
> the controller). It's meant to be invoked through script/runner, so I
> won't have a response object.
>
> I can see in the plugin source that there is assert_select_email
> support, but I'm pretty sure there is no expectation that starts with
> "assert." How would one spec something like:
>
> setup do
>    ActionMailer::Base.delivery_method = :test
>    ActionMailer::Base.perform_deliveries = true
>    ActionMailer::Base.deliveries = []
>    MyMailer.trigger_emails   # invokes model method to send 4 emails
> end
>
> specify "d'oh! should have a non-empty recipient list" do
>    ActionMailer::Base.deliveries.size.should be(4)
>    ActionMailer::Base.deliveries.each do |email|
>      email.recipients.should_not be_empty
>    end
> end

assert_select doesn't wrap this sort of access, so neither does the
port. What you've got there is probably what I'd do. Anything that
RSpec would provide would wrap that anyhow.

One thing you can do slightly differently is this:

  ActionMailer::Base.should have(4).deliveries

That's in rspec core.

>
> specify "first email should have a list item with the name of the
> first guy" do
>    ActionMailer::Base.deliveries[0].should select_tag('li', members
> (:joe).name)
> end

This one you shold be able to do like this:

specify "first email should have a list item with the name of the first guy" do
  response.should be_email {
    with_tag('li', members(:joe).name)
  }
end

Hope that helps a little.

David

>
>
> The above code is truly bogus and doesn't work. I'm trying to figure
> out where to get the messages and how to specify the expectations on
> them.
>
> Thanks,
>
> Steve
>
>
> On Feb 4, 2007, at 1:35 PM, David Chelimsky wrote:
>
> > Steve - are you using the RSpec trunk?
> >
> > If you are, AND your email is html email, you can use this right now:
> >
> > response.should send_email {
> >   with_tag("div", "joe at shmoe.com")
> >   with_tag("div", projects(:trace).name)
> >   with_tag("div", member/show)
> > }
> >
> > There is no specific support yet for email headers, nor raw text,
> > though I can see now there is a need for that and will raise the
> > appropriate RFE.
> >
> > David
> >
>
>
>
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>


More information about the rspec-users mailing list