Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我知道有一种方法可以让存根返回多个不同的值,如下所示:
subject.stub(:method_call.and_return(1,2,3)
但我希望这样的事情是可能的:
subject.stub(:method_call).and_raise(Exception).once subject.stub(:method_call).and_return(1)
但我还没有找到一种优雅的方式让存根只在第一次调用时引发异常。建议?
我知道这样做的唯一方法是使用这样的计数器变量:
counter = 0 times = 2 TestModel.any_instance.stub(:some_method) do (counter += 1) <= times ? raise(Exception) : 1 end
这将输出如下:
test = TestModel.new test.some_method => Exception test.some_method => Exception test.some_method => 1