Rails / Rspec:.should_receive(method).with(参数)其中参数在调用测试方法之前是未知的

时间:2013-06-24 10:06:39

标签: ruby-on-rails rspec

我有一个创建新对象的方法,并以该新对象作为参数调用服务,并返回新对象。我想在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

但我不能,因为在obj返回之前我不知道method_to_test

1 个答案:

答案 0 :(得分:3)

根据检查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_test
相关问题