Rspec即使不应该发送电子邮件-实现delivery_later吗?

时间:2019-07-26 10:36:05

标签: ruby-on-rails rspec

我有一份邮件工作,应该在2天,10天和26天后发送。在其他任何天数(例如6)之后,都不应再发送电子邮件。在本地可以使用,但是rspec在下面的测试中失败

RSpec.describe RemindUnconfirmedUsersWithoutBanker do
  subject(:remind_unconfirmed_users) { described_class.call(number_of_days) }

  let(:number_of_days) { [2, 10, 26] }

  before do
    ActiveJob::Base.queue_adapter = :test
    allow(InvitationMailer).to receive_message_chain(:confirmation_instructions, :deliver)
  end

  context "when created_at equals to 6 days" do
    it "does not invite user again" do
      create_list(:user, 2, created_at: 6.days.ago, confirmed_at: nil)
      remind_unconfirmed_users
      expect(InvitationMailer).not_to have_received(:confirmation_instructions)
    end
  end
end

有错误

  

1.1)错误/错误:期望(InvitationMailer).not_to have_received(:confirmation_instructions)

   (InvitationMailer (class)).confirmation_instructions(no args)
        expected: 0 times with any arguments
        received: 1 time
  # ./spec/services/remind_unconfirmed_users_without_banker_spec.rb:52:in `block (3 levels) in <top (required)>'
     

1.2)错误/错误:期望(InvitationMailer).not_to have_received(:confirmation_instructions)

  (InvitationMailer (class)).confirmation_instructions(*(any args))
        expected: 0 times with any arguments
        received: 2 times with any arguments
  # >./spec/services/remind_unconfirmed_users_without_banker_spec.rb:52:in `block (3 levels) in <top (required)>'

修改

我认为我必须以某种方式实现deliver_later,因为我的服务具有此方法

remind_unconfirmed_users_without_banker.rb

  def resend_invitation(users_to_mail)
    users_to_mail.each do |user|
      Devise::Mailer.confirmation_instructions(user, user.confirmation_token).deliver_later
    end
  end

Edit2

如果我删除confirmed_at: nil并离开create_list(:user, 2, created_at: 6.days.ago),则测试将通过。在这种情况下,空白字段和​​nil会不同吗?

0 个答案:

没有答案