我有一个方法,它创建一个新对象,并使用该新对象作为参数调用服务,然后返回新对象。我想在rspec中测试该方法是否使用创建的对象调用服务,但我不知道提前创建的对象。我是否也应该将对象创建存根?
def method_to_test
obj = Thing.new
SomeService.some_thing(obj)
end我想写下:
SomeService.stub(:some_thing)
SomeService.should_receive(:some_thing).with(obj)
method_to_test但是我不能因为在method_to_test回来之前我不知道obj ..。
发布于 2013-06-24 18:32:28
根据检查obj是否重要,您可以执行以下操作:
SomeService.should_receive(:some_thing).with(an_instance_of(Thing))
method_to_test或者:
thing = double "thing"
Thing.should_receive(:new).and_return thing
SomeService.should_receive(:some_thing).with(thing)
method_to_testhttps://stackoverflow.com/questions/17272867
复制相似问题