How to test with Rspec that `discard_on` is called, with rails 6?

Viewed 789

I have a job, which should discard a specific error:

class SomeJob < ApplicationJob
discard_on(Errno::ENOENT) do |job, error|
    ...do things...
   end

   def perform(**args)
     ...do things...
   end
end

I have tried, based on How to properly test ActiveJob's retry_on method with rspec? to mock test it like that:

context '#discard_on' do
      it 'should discard on Errno::ENOENT error' do
        expect_any_instance_of(described_class).to receive(:perform).and_raise(Errno::ENOENT.new)
        expect(SomeJob).to have_received(:discard_on)

        UploadJob.perform_now(**args)
      end
    end

but I am keep getting the following error:

 #<SomeJob (class)> expected to have received discard_on, but that object is not a spy or method has not been stubbed.

If I try it like that:

expect_any_instance_of(SomeJob).to receive(:perform).and_raise(Errno::ENOENT)
expect_any_instance_of(described_class).to receive(:discard_on)

SomeJob.perform_now(**args)

It fails with this error:

SomeJob does not implement #discard_on
0 Answers
Related