Testing that Devise is re-sending confirmation instructions email in a background worker

核能气质少年 提交于 2020-02-25 01:30:02

问题


I want to send Devise confirmation instructions to users a second time if they haven't confirmed within two days of signing up, however I can't seem to get my success case test to pass.

Background worker (runs once a day):

class ResendConfirmationWorker
  include Sidekiq::Worker
  sidekiq_options queue: :resend_confirmation, retry: false

  def perform
    d = Time.zone.now - 2.days
    users = User.where.not(confirmation_sent_at: nil)
                .where(confirmed_at: nil)
                .where(created_at: d.beginning_of_day..d.end_of_day)

    users.find_each do |user|
      user.send_confirmation_instructions
    end
  end
end

RSpec test:

require 'rails_helper'

describe ResendConfirmationWorker, type: :job do
  before do
    time = Time.zone.now - 2.days
    @user = create :unconfirmed_user,
                    created_at: time,
                    confirmation_sent_at: time
  end

  def run_job
    subject.perform
  end

  it 'resends a confirmation email to people who haven’t accepted it within two days' do
    run_job
    expect(Devise::Mailer.deliveries.count).to eq 1
  end
end

I always get 0 instead of 1. I've also tried looking at Sidekiq::Extensions::DelayedMailer.jobs.size and Devise.mailer.deliveries.count but they also return 0.

Lastly, I put a binding in the worker and run user.send_confirmation_instructions manually:

[1] pry(#<ResendConfirmationWorker>)> user.send_confirmation_instructions
=> #<ActionMailer::DeliveryJob:0x007fc53f05c420
 @arguments=
  ["Devise::Mailer",
   "confirmation_instructions",
   "deliver_now",
   #<User id: 1, email: "unconfirmed1@example.com", created_at: "2017-01-05 04:40:13", updated_at: "2017-01-07 04:40:13", company_id: nil, name: "Unconfirmed User", invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_type: nil, invited_by_id: nil, invitations_count: 0>,
   "token1",
   {}],
 @job_id="9b5f6231-3194-4c57-9a6b-a38368cec603",
 @priority=nil,
 @queue_name="mailers">

It looks like it's correctly adding a new confirmation instructions email to the mailer queue, so why can't I see it from my test?

来源:https://stackoverflow.com/questions/41517881/testing-that-devise-is-re-sending-confirmation-instructions-email-in-a-backgroun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!