rubymockingstubruby-mocha

Unstubbing a class method in Mocha


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.


Solution

  • 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 ....