ruby - Rspec Expect Child Method Invocation Without Causing Parent Method To Fail -
i able test method being called inside method, without testing else.
let's assume have method makes internal service call.
class foo def self.perform result = internalservice.call ... result.attribute_method # stuff result end end
internalservice
has of own unit tests, , don't want duplicate tests here. however, still need test internalservice
being called.
if use rspec's expect
syntax, mock out internalservice.call
, rest of method fail because there no result.
allow_any_instance_of(internalservice).to receive(:call).and_return(result) foo.perform => nomethoderror: => undefined method `attribute_method'
if use rspec's allow
syntax explicitly return result, expect
clause fails because rspec has overridden method.
allow_any_instance_of(internalservice).to receive(:call).and_return(result) expect_any_instance_of(internalservice).to receive(:call) foo.perform => failure/error: unable find matching line backtrace => 1 instance should have received following message(s) didn't: call
how can test method being called on object? missing bigger picture here?
try this:
expect(internalservice).to receive(:call).and_call_original foo.perform
it's class
method, right? if not, replace expect
expect_any_instance_of
.
more and_call_original
can found here.
Comments
Post a Comment