如果用户在注册后的两天内没有得到确认,我想第二次向用户发送设计确认说明,但我似乎无法通过我的成功案例测试。
后台工作人员(每天运行一次):
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测试:
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
我总是得到0而不是1.我还尝试查看Sidekiq::Extensions::DelayedMailer.jobs.size
和Devise.mailer.deliveries.count
,但他们也返回0.
最后,我在工作人员中放置一个绑定并手动运行user.send_confirmation_instructions
:
[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">
看起来它正确地将新的确认说明电子邮件添加到邮件程序队列中,为什么我无法在测试中看到它?