Rspec 没有发送带有 Json post 请求的电子邮件

Rspec not sending out emails with Json post request

我的规格:

      context 'with valid data' do
        describe 'refers other users' do
          before(:each) do
            post '/shop/referrals', valid_referral_params
            ActionMailer::Base.perform_deliveries = true
            ActionMailer::Base.deliveries.clear
          end

        def valid_referral_params
          { "shop_referral"=>{"sender_email"=>"sender@hotmail.com", "receiver_email_one"=>"myfriend@hotmail.com",
          "receiver_email_two"=>"myfriendd@hotmail.com", "receiver_email_three"=>"myfriendddddd@hotmail.com"}, format: :json }
        end
      end

以下测试未通过:

  it { expect(ActionMailer::Base.deliveries.size).to eq 4 }

几乎我发送了 4 封电子邮件,目前 expected 4 got 0,当我删除 ActionMailer::Base.deliveries.clear 时,我收到 expected 4 got 12

来自我的模型:

def send_notifications
  ReferralMailer.referral_sender_email(sender_email).deliver!
  reciever_email_collection.each do |email|
    ReferralMailer.referral_receiver_email(email).deliver!
  end
end

reciever_email_collection = [myfriend1@hotmail.com, myfriend2@hotmail.com, myfriend3@hotmail.com]

还有
config.action_mailer.delivery_method = :test 在配置中。

如有任何帮助,我们将不胜感激。谢谢!

我没有尝试重现该问题,但在我看来您在 post 请求后正在清除邮件队列。您是否尝试过在 post 之前移动清除?