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

2014-08-12

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:

I’m currently working on fyra.sh, a CLI-first static site deployment tool where you push your site and it’s served globally through a built-in CDN, without the overhead of heavy platforms.