加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – rspec嘲笑ActionMailer

发布时间:2020-12-17 02:03:47 所属栏目:百科 来源:网络整理
导读:我正在写一些rspec测试,我注意到它在访问我的Mailer类时速度变慢了.有没有办法完全模拟ActionMailer :: Base类,所以我可以在电子邮件传递之前和之后测试我的控制器的其他组件? 这是我的邮件程序类定义 class OrganisationMailer ActionMailer::Base# code t
我正在写一些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)

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读