Home > OS >  rails rspec how to know if method is called from worker perform
rails rspec how to know if method is called from worker perform

Time:12-09

I hav this worker

class Aworker
  def perform(x_id)
    method_c
  end

  def method_c
   # ...
  end

and in my spec file

RSpec.describe Aworker, type: :worker do
  describe ".perform" do
  x = FactoryBot.create(:x)
  expect(described_class.new.perform(x.id))to receive(method_c)
  end
end

but I get this error: ArgumentError: Cannot proxy frozen objects, rspec-mocks relies on proxies for method stubbing and expectations.

I have followed some tutorials but I'm not sure if workers are tested differently

CodePudding user response:

Should look something like that:

RSpec.describe Aworker, type: :worker do
  describe ".perform" do
    it 'performs' do
      x = FactoryBot.create(:x)
      instance = described_class.new
      expect(instance).to receive(:method_c)
      instance.perform(x.id)
    end
  end
end
  • Related