David - Musings of an SRE

DRY your ActionMailer::Base.deliveries.clear in RSpec tests

When testing for mailers, you might need to add the following quite frequently:

  describe "test mailer" do
    it "should work" do
      ActionMailer::Base.deliveries.clear

      # bunch of other
      # codes here
    end
  end

You probably would add them at the start of every test block that tests for ActionMailer.

The neater way would be to do this:

  describe "test mailer", type: :mailer do
    it "should work" do
      # bunch of
      # codes here
    end
  end

By assigning a mailer_type to the test describe, RSpec will automatically clear the actionmailer deliveries at the start.

Dry yourself!

References: