小编Oct*_*ock的帖子

如何使用rspec正确测试ActiveJob的retry_on方法?

在过去的几天里,我一直在尝试测试这种方法,但是没有运气。

我想做的另一件事是rescue在尝试最后一次重试后冒出的错误。

请在下面查看我的评论和代码段。

retry_on的源代码也位于上下文中。

这是示例代码和测试:

   my_job.rb

   retry_on Exception, wait: 2.hours, attempts: 3 do |job, exception|
   # some kind of rescue here after job.exceptions == 3  
   # then notify Bugsnag of failed final attempt.
   end

   def perform(an_object)
     an_object.does_something
   end

   my_spec.rb
   it 'receives retry_on 3 times' do
     perform_enqueued_jobs do
       expect(AnObject).to receive(:does_something).and_raise { Exception }.exactly(3).times
       expect(MyJob).to receive(:retry_on).with(wait: 2.hours, attempts: 3).exactly(3).times
       MyJob.perform_later(an_object)
     end
     assert_performed_jobs 3
   end
Run Code Online (Sandbox Code Playgroud)

测试失败响应:

      1) MyJob.perform receives retry_on 3 times
         Failure/Error: expect(job).to receive(:retry_on).with(wait: 4.hours, attempts: 3).exactly(3).times

   (MyJob (class)).retry_on({:wait=>2 hours, …
Run Code Online (Sandbox Code Playgroud)

rspec ruby-on-rails rspec-rails rails-activejob ruby-on-rails-5.1

11
推荐指数
3
解决办法
1960
查看次数