RSpec: Stubbing out calls for certain parameters
I want to stub out a method only for a certai开发者_Go百科n parameter. Say I have a class
class Foo
def bar(i)
i*2
end
end
Now I want to stub out the method bar only for calls with a value of say 3 and return the method return value in all other cases:
>> foo = Foo.new
>> foo.bar(2)
=> 4
>> foo.stub!(:bar).with(3).and_return(:borked)
>> foo.bar(3)
=> :borked
>> foo.bar(2)
NoMethodError: undefined method `bar' for #<Foo:0x10538e360>
Is there a way to delegate execution to the method being stubbed out?
You can use unstub!
method
>> foo = Foo.new
>> foo.bar(2)
=> 4
>> foo.stub!(:bar).with(3).and_return(:borked)
>> foo.bar(3)
=> :borked
>> foo.unstub!(:bar)
>> foo.bar(2)
NoMethodError: undefined method `bar' for #<Foo:0x10538e360>
精彩评论