Testing SandboxEmailInterceptor Initializer in Rails 5

I recently needed to implement some tests for the SandboxEmailInterceptor Intializer pattern suggested by the rails guide. I found two methods to test the class but neither of them are perfect.

You can test the functionality of the interceptor

test 'it should redirect email when interceptor is run' do
  SandboxEmailInterceptor.delivering_email(@email)
  assert_equal @email.to, ['sandbox@example.com']
end

However, passing this test doesn’t mean your emails will be intercepted.

Stub the environment and load the initializer

test 'it sends all emails to staging@donorsiblingregistry.com in staging' do
  Rails.stub(:env, ActiveSupport::StringInquirer.new('staging')) do
    load 'config/initializers/sandbox_email_interceptor.rb'

    @email.deliver

    last_email_sent = ActionMailer::Base.deliveries.last
    assert_equal last_email_sent.to, ['sandbox@example.com']
  end
end

This gets us closer but if your initializers aren’t running, for example then this test will still be inadequate. I haven’t found a better solution than this one so please comment if you know of one.


Thank you

Your comment has been submitted and will be published once it has been approved.

OOPS!

Your comment has not been submitted. Please go back and try again. Thank You!

If this error persists, please open an issue by clicking here.

Say something