DEV Community

Alexandra Ackerman
Alexandra Ackerman

Posted on

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 in a controller or service 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("address@email.com").to have_received_email(body: "Wonderful email body")
expect(some_user).to have_received_email(subject: "My great subject") 

Check it out and feel free to contribute by opening an issue or pull request!

Latest comments (0)