RSpec模拟ActionMailer

4

我正在编写一些rspec测试,发现访问我的Mailer类时速度变慢。有没有一种方法可以完全模拟ActionMailer::Base类,以便在电子邮件发送之前和之后测试控制器的其他组件?

这是我的Mailer类定义:

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
1个回答

1
这个问题很难根据你提供的代码片段来回答,但是你应该能够在RSpec示例中将控制器发送到OrganisationMailer的任何消息存根化,以便在你不想测试该逻辑的情况下,它们成为无操作。
或者,你可以使用stub_const替换OrganisationMailer为一个测试替身。
stub_const("OrganisationMailer", my_test_double)

这样你就可以完全控制控制器和邮件发送程序之间的交互。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接