ruby on rails - how to test that service object is initialized with the correct params? -
in rails app have background job calls service object
class myjob < activejob::base   def perform( obj_id )     return unless object = object.find(obj_id)     myserviceobject.new(object).call   end end i can test job calls service object follows:
describe myjob, type: :job   let(:object) { create :object }   'calls myserviceobject'     expect_any_instance_of(myserviceobject).to receive(:call)     myjob.new.perform(object)   end end but how test job initializes service object correct params?
describe myjob, type: :job   let(:object) { create :object }   'initializes myserviceobject object'     expect( myserviceobject.new(object) ).to receive(:call)     myjob.new.perform(object)   end end i want achieve above, fails expects 1 received 0. 
what correct way test class initialized correctly?
apparently fix adilbiy's answer turned down, here's updated answer:
describe myjob, type: :job   let(:object) { create :object }   'initializes myserviceobject object'     = myserviceobject.new(object)     expect(myserviceobject).to receive(:new).with(object).and_return(so)     myjob.new.perform(object)   end end by mocking myserviceobject.new expect(myserviceobject).to receive(:new).with(object), overwrite original implementation. however, myjob#perform work, need myserviceobject.new return object - can .and_return(...).
hope helps!
Comments
Post a Comment