Comments (4)
Hi Jakub. Interesting idea. However, I can't really see why it would be useful. Why wouldn't you just split the test into two tests with a different expectation in each? Do you have a concrete example where this idea would be useful? Thanks for your interest in Mocha. Cheers, JM.
from mocha.
Hi, sometimes when I call two methods that call another one, I have to set the .times()
for the expectation, and it's not specific as to which method calls the other one, for example
a
callsc
onceb
callsc
twice.expects(:c).times(3)
when a
and b
change, a
can call c
three times and b
can never call c
, so the behaviour is very different, but the test still passes.
I guess splitting the test is a good way to go, but I was just curious if there's a possibility in mocha right now for that.
from mocha.
So if I understand you correctly, you're saying that given a class and test like this :-
class MyClass
def initialize(collaborator)
@collaborator = collaborator
end
def method_one
1.times { @collaborator.foo }
end
def method_two
2.times { @collaborator.foo }
end
def method_three
method_one
method_two
end
end
require "test/unit"
require "mocha"
class MyClassTest < Test::Unit::TestCase
def test_method_three
collaborator = mock("collaborator")
my_object = MyClass.new(collaborator)
collaborator.expects(:foo).times(3)
my_object.method_three
end
end
I can change MyClass#method_one
and #method_two
as follows :-
def method_one
2.times { @collaborator.foo }
end
def method_two
1.times { @collaborator.foo }
end
And the test will still pass. Is that what you mean?
I think the way it currently works makes sense to me. The test only cares how many times collaborator#foo
is called it does not care which method calls it. If #method_one
and #method_two
call #foo
with different parameters, then you might need to be more specific with your expectations, i.e. use Mocha::Expectation#with to constrain the expected invocations of #foo
.
If you care about how many times #method_one
calls #foo
then maybe you should have a test that only calls #method_one
and then you can have an expectation for a specific number of invocations.
You might also want to look at the Mocha::StateMachine examples e.g. Mocha::Expectation#when, Mocha::Expectation#then. This might allow you to do something like what you are talking about.
So far I haven't been convinced to make any changes to Mocha. If you are happy with the above explanation, can you close this issue? Otherwise you need to try harder to convince me that Mocha is missing some important functionality!
from mocha.
You did understand me correctly and both solutions that you've provided are great. I wasn't aware of the StateMachine implementation, I'll have to take a closer look at it. expect
blocks really seem useless now.
Thanks for your time and a lengthy response
from mocha.
Related Issues (20)
- Add sitemap to docs site to reduce crawl errors
- Upgraded from 1.0 to 2.0. HOT 7
- Config option for blocking mocks/stubs on specific methods or classes HOT 1
- Mocha is incompatible with minitest v5.19 and later HOT 16
- Replace references to `MiniTest` with `Minitest` and generally tidy this stuff up HOT 1
- Consider adding an option to disable backtrack filtering
- Unstub in the `then` clause
- Stub by id
- Mocha::StateMachine states method stomps on states fixture HOT 8
- Stubs on instances of DelegateClass do not behave as expected HOT 9
- Add docs about issues & workarounds for using Mocha with BasicObject & the various Delegator classes
- YARD docs for Mock#method_missing is broken HOT 1
- Update Ruby Hash syntax in docs
- Drop support for Ruby v2.0
- Drop support for Ruby v2.1
- Document some of the history of Mocha
- Improve failure message
- sometimes method fails to be mocked HOT 8
- Sign releases
- Stubbed methods not cleaned up post spec completion HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mocha.