Rspec mocks ActionMailer

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

Here is the mailer class definition

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

Here is one of the tests I wrote

 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 
+4
source share
1 answer

It is difficult to answer with the small snippet that you have included, but you should be able to disable any messages that your controller sends to the OrganisationMailer so that they are not operations in the rspec examples where you do not follow this logic.

Alternatively, you can watch replacing the 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 mail program.

+1
source

All Articles