RubyFlow The Ruby and Rails community linklog

Better ActionMailer testing with ActionMailerMatchers

ActionMailerMatchers provides you with RSpec one-liners that help you to test sending emails in your Rails application.

It is typical to test that an email has been delivered by doing the following:

expect { some_action.execute }.to change { ActionMailer::Base.deliveries.count }.by(1)

However, this does not specify what email was sent and to where. ActionMailerMatchers solves this problem by providing the have_received_email matcher, which ensures that the passed email address or user (which must respond .email) was the email’s “to” address. You may also use the optional subject and body arguments to check that content matches the email you were expecting to send.

expect(some_user).to have_received_email(subject: "My great subject") expect("address@email.com").to have_received_email(body: "Wonderful email body")

Comments

Wow, I was working on mailers last week and this is exactly what I need! Thanks for open-sourcing it!

Post a comment

You can use basic HTML markup (e.g. <a>) or Markdown.

As you are not logged in, you will be
directed via GitHub to signup or sign in