Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use mocha to stub method without any expectation

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?

like image 244
dagda1 Avatar asked Oct 26 '25 07:10

dagda1


1 Answers

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

like image 76
phoet Avatar answered Oct 28 '25 23:10

phoet