我有这个代码用Rspec来测试ActiveJob和ActionMailer
我不知道真正执行所有入职的工作
我不知道真正执行所有入职的工作
describe 'whatever' do include ActiveJob::TestHelper after do clear_enqueued_jobs end it 'should email' do expect(enqueued_jobs.size).to eq(1) end end
解决方法
这是我如何解决类似的问题:
# rails_helper.rb RSpec.configure do |config| config.before :example,perform_enqueued: true do @old_perform_enqueued_jobs = ActiveJob::Base.queue_adapter.perform_enqueued_jobs @old_perform_enqueued_at_jobs = ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs = true end config.after :example,perform_enqueued: true do ActiveJob::Base.queue_adapter.perform_enqueued_jobs = @old_perform_enqueued_jobs ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs = @old_perform_enqueued_at_jobs end end
然后在规格我们可以使用:
it "should perform immediately",perform_enqueued: true do SomeJob.perform_later end