Skip to content

Instantly share code, notes, and snippets.

@masonforest
Created May 17, 2013 22:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save masonforest/5602280 to your computer and use it in GitHub Desktop.
Save masonforest/5602280 to your computer and use it in GitHub Desktop.
rspec matcher for ActionMailer rails email delivery
# Usage
# order = create(:order)
#
# expect {
# order.pay!
# }.to deliver_email(OrderMailer, :paid, order.id)
#
#
RSpec::Matchers.define :deliver_email do |mailer, action, *args|
match do |block|
stubbed_mailer = double('mailer', deliver: true)
mailer.stub(action => stubbed_mailer)
block.call
expect(mailer).to have_received(action).with(*args)
expect(stubbed_mailer).to have_received(:deliver)
end
failure_message_for_should do |actual|
"expected that block would deliver #{mailer}##{:action}"
end
failure_message_for_should_not do |actual|
"expected that block would not deliver #{mailer}##{:action}"
end
description do
"deliver #{mailer}##{:action}"
end
end
@ivandenysov
Copy link

You've got error in failure messages and description
"#{:action}" should be "#{action}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment