繁体   English   中英

rspec嘲笑ActionMailer

[英]rspec mocking out ActionMailer

我正在写一些rspec测试,我注意到它在访问我的Mailer类时速度变慢了。 有没有办法完全模拟ActionMailer :: Base类,所以我可以在电子邮件传递之前和之后测试我的控制器的其他组件?

这是我的邮件程序类定义

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

这是我写的一个测试

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

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

或者,您可以使用stub_const查看使用测试双重替换OrganisationMailer

stub_const("OrganisationMailer", my_test_double)

然后,您可以完全控制控制器和邮件程序之间的交互。

暂无
暂无

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

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