1

For a particular test, I want to change the return value of a class method.

I can get the correct behavior by calling MyClass.expects(:method).returns(:myvalue). How can I stop this behavior once I'm done with the test?

There's an unstub method in Mocha, but it appears to only work on instance methods, not class methods.

Vega
  • 27,856
  • 27
  • 95
  • 103
Craig Walker
  • 49,871
  • 54
  • 152
  • 212

1 Answers1

2

What version number of mocha are you using?

This works in MRI / mocha 0.9.12:

class T
  def self.hello
    "hi"
  end
end

T.hello # => "hi"
T.expects(:hello).returns("hello")
T.hello # => "hello"
T.unstub(:hello)
T.hello # => "hi"
T.expects(:hi).returns("world")
T.hi    # => "world"
T.unstub(:hi)
T.hi    # => NoMethodError: undefined method ....
Brandon
  • 2,574
  • 19
  • 17
  • That's the trick: I'm using mocha-0.9.8. I misread the 0.9.12 docs to mean that they were instance-only, and misinterpreted my error message as confirmation of that. Thanks. – Craig Walker Mar 14 '11 at 21:53