4
votes

I'm writing some rspec tests and I notice that it slows down when accessing my Mailer class. Is there a way to mock out the ActionMailer::Base class completely so I can test other components of my controller before and after the email delivery?

Here is my mailer class definition

class OrganisationMailer < ActionMailer::Base
# code to send emails
end

Here is one of the tests I've written

require 'spec_helper'

describe OrganisationsController do

  describe "#email_single" do
    context "With email address" do 
      let(:org) {Organisation.make!}

      it "redirects to listing" do
        get :email_single, :id => org.id
        response.should redirect_to(organisations_path)
      end
    end
  end
end
1

1 Answers

1
votes

This is hard to answer with the little snippet you've included, but you should be able to stub out whatever messages your controller sends to OrganisationMailer so that they are no-ops in the rspec examples where you don't care to exercise that logic.

Alternately, you can look at replacing OrganisationMailer with a test double using stub_const:

stub_const("OrganisationMailer", my_test_double)

Then you can completely control the interaction between the controller and the mailer.