How do you ensure expects is invoked exactly `n` times
Bennet-Sunder opened this issue · comments
I noticed that when using times
returns true even if it's invoked more times than required. like it returns true if greater than 3, but false if lesser than 3
object = mock()
object.expects(:expected_method).times(3)
10.times { object.expected_method }
# => verify succeeds
Is there a way I can use exactly n
times? I tried using the https://www.rubydoc.info/github/floehopper/mocha/Mocha/Cardinality#exactly-class_method but I get an exception saying
undefined method `exactly' for #<Mocha::Expectation
How can I achieve exactly n
times? It looks like exactly is used only for twice
and once
https://github.com/floehopper/mocha/blob/master/lib/mocha/expectation.rb
Unfortunately, although you've opened this issue against the correct repo, your comment is referring to code in an old deprecated repo (https://github.com/floehopper/mocha) as explained in its README which is stuck at v0.11.3. You're also looking at the documentation associated with that old deprecated repo (rubydoc.info/github/floehopper/mocha).
The canonical repo is this one (https://github.com/freerange/mocha) which has the code at v1.12.0 and the canonical documentation is here (https://mocha.jamesmead.org/).
Assuming you're using a reasonably recent version of Mocha, passing a single number to Mocha::Expectation#times
should do what you want.
Out of interest how did you find that out-of-date repo / docs? Was it via a search engine? I've created a new issue to archive the old repo and request the removal of the docs.
I'm going to close this issue now, but please re-open if this doesn't solve your problem.
Thanks. Yes, I did find it via google search