2012-12-29 40 views
4

我正在写一些RSpec的测试,我发现它访问我的梅勒类时减慢。有没有一种方法可以完全模拟ActionMailer :: Base类,以便在电子邮件传递之前和之后测试控制器的其他组件?RSpec的嘲讽出来的ActionMailer

这里是我的邮件类定义

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

这是很难与你已经包括了小片段来回答,但你应该能够将你的控制器发送给OrganisationMailer的任何消息进行分类,以便它们在rspec示例中无操作,您不需要执行该逻辑。

或者,也可以看与双用stub_const测试替换OrganisationMailer

stub_const("OrganisationMailer", my_test_double) 

然后就可以完全控制在控制器和邮件收发器之间的相互作用。