tags:

views:

68

answers:

1

I want to stub out a method only for a certain 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?

+1  A: 

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>
shingara