简体   繁体   English

rspec嘲笑ActionMailer

[英]rspec mocking out ActionMailer

I'm writing some rspec tests and I notice that it slows down when accessing my Mailer class. 我正在写一些rspec测试,我注意到它在访问我的Mailer类时速度变慢了。 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? 有没有办法完全模拟ActionMailer :: Base类,所以我可以在电子邮件传递之前和之后测试我的控制器的其他组件?

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

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. 使用你所包含的小片段很难回答这个问题,但你应该能够将你的控制器发送给OrganisationMailer任何消息存根,以便它们在rspec示例中是无操作的,你不关心这些逻辑。

Alternately, you can look at replacing OrganisationMailer with a test double using stub_const : 或者,您可以使用stub_const查看使用测试双重替换OrganisationMailer

stub_const("OrganisationMailer", my_test_double)

Then you can completely control the interaction between the controller and the mailer. 然后,您可以完全控制控制器和邮件程序之间的交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM