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

ruby-on-rails – 如何编写一个Rspec控制器测试,确保发送电子邮

发布时间:2020-12-16 22:38:58 所属栏目:百科 来源:网络整理
导读:目前在我的控制器规格我有: require 'spec_helper'describe CustomerTicketsController do login_user describe "POST /create (#create)" do # include EmailSpec::Helpers # include EmailSpec::Matchers it "should deliver the sales alert email" do #
目前在我的控制器规格我有:
require 'spec_helper'

describe CustomerTicketsController do
  login_user

  describe "POST /create (#create)" do
    # include EmailSpec::Helpers
    # include EmailSpec::Matchers
    it "should deliver the sales alert email" do
      # expect
      customer_ticket_attributes = FactoryGirl.attributes_for(:customer_ticket)
      customer_mailer = mock(CustomerMailer)
      customer_mailer.should_receive(:deliver).
        with(CustomerTicket.new(customer_ticket_attributes))
      # when
      post :create,:customer_ticket => customer_ticket_attributes
    end
  end
end

在我的控制器我有:

# POST /customer_tickets
  # POST /customer_tickets.xml
  def create
    respond_to do |format|
      if @customer_ticket.save
        CustomerMailer.sales_alert(@customer_ticket).deliver
        format.html { redirect_to @customer_ticket,notice: 'Customer ticket was successfully created.' }
        format.xml { render xml: @customer_ticket,status: :created,location: @customer_ticket }
      else
        format.html { render action: "new" }
        format.xml { render xml: @customer_ticket.errors,status: :unprocessable_entity }
      end
    end
  end

我的测试目前产生以下输出:

06002

谢谢你的看.

解决方法

您可以检查ActionMailer :: Base.deliveries.count以确保其递增1.

这样的东西(未经测试)

expect {custom_mailer.deliver}.to change { ActionMailer::Base.deliveries.count }.by(1)

(编辑:李大同)

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

    推荐文章
      热点阅读