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

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 如何编写一个Rspec控制器测试,确保发送电子邮件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
目前在我的控制器规格我有:
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)
原文链接:https://www.f2er.com/ruby/265758.html

猜你在找的Ruby相关文章