[rspec-users] feedback on matcher

Mike Mazur mmazur at gmail.com
Thu Jan 27 18:31:01 EST 2011


Hi,

On Thu, Jan 27, 2011 at 21:48, Michael Guterl <mguterl at gmail.com> wrote:
> RSpec::Matchers.define :deliver do |message|
>  chain :with do |*args|
>    @with = args
>  end
>
>  match do |mailer|
>    mail = double
>    mail.should_receive(:deliver)
>
>    mailer.should_receive(message).with(*@with).and_return(mail)
>  end
> end
>
> Mailer.should deliver(:job_application).with(@job.id, @user.id)

On a recent Rails 3 project we implemented a matcher for sending email
but without using any stubs. It looks something like this:

  RSpec::Matchers.define :send_mail do
    match do |block|
      ActionMailer::Base.deliveries = []
      block.call
      ActionMailer::Base.deliveries.count.should == 1
      mail = ActionMailer::Base.deliveries.last
      # now inspect the mail object as you see fit
    end
  end

Then in our spec we have:

  expect { some_func }.to send_email

We also have chains for recipients, subject, content, and so on.

HTH,
Mike


More information about the rspec-users mailing list