3

I am stubbing a method like this in the setup of one of my tests

  def setup
    super
    #blah, blah
    GoogleIdentity.stubs(:new).with(google_identity).returns(google_account)
  end

The problem is that not every test will invoke the method and return the object.

In the methods that do not invoke the method, I get this error:

allowed any number of times, not yet invoked: GoogleIdentity.new()

It seems like mocha is complaining because the method was not invoked.

How can I specify a stub that does not expect it to be calle?

Vega
  • 27,856
  • 27
  • 95
  • 103
dagda1
  • 26,856
  • 59
  • 237
  • 450
  • 1
    Did Mocha change this? "A stub is just an expectation of zero or more invocations." http://gofreerange.com/mocha/docs/ – mahemoff Apr 28 '14 at 23:24

1 Answers1

3

I think that the implementation of mocha is broken. A method called stubs should not care about being called or not. That should be the responsibility of a mocks method.

This behavior is one of the things I don't use mocha anymore.

From what I remember when fixing this issue, I used one of the expectations to handle this scenario like at_most(1).

phoet
  • 18,688
  • 4
  • 46
  • 74