Comments (13)
Yes, it should.
from mockito.
I agree this is annoying. I recently converted a lot of code that used the deprecated mock package to use mockito. The mock package is much looser about function arguments, so it was frustrating to migrate to mockito, and then tests don't work until I hunt down the exact method call that I want to match against.
logInvocations
and throwOnMissingStub
can help situations like this.
However, if you are also referring to the issue where the calling code has all the different variations, your test setUp can have tons of boilerplate! Like what if I want to mock HttpRequest.open?
class MockHR extends Mock implements HttpRequest {}
var mhr = new MockHR();
when(mhr.open(any, any)).thenReturn(...);
when(mhr.open(any, any, async: any)).thenReturn(...);
when(mhr.open(any, any, user: any)).thenReturn(...);
when(mhr.open(any, any, password: any)).thenReturn(...);
when(mhr.open(any, any, async: any, user: any)).thenReturn(...);
when(mhr.open(any, any, async: any, password: any)).thenReturn(...);
when(mhr.open(any, any, user: any, password: any)).thenReturn(...);
when(mhr.open(any, any, async: any, user: any, password: any)).thenReturn(...);
Ugh!
We could maybe introduce a looser mocking call, a sibling to when
. Maybe whenLoosely
or whenSomethingLike
? Or maybe another intermediate call like when(mhr.open(any, any)).dontworryaboutotherargumentsthough.thenReturn(...)
.
WDYT @matanlurey
from mockito.
logInvocations definitely helps but then it's a bit backwards. You're can't write the expected behaviour in a test first then do the implementation. It's more like running it once then saving a golden. The scoping would mean when you refactor unrelated things, you'll have to keep updating the goldens to match different signatures even though it doesn't affect the real logic you wanted to test.
from mockito.
Hi @xster, sorry for the long delay. WDYT about my two ideas? Either
whenLoosely(obj.a()).thenReturn(...);
// or
when(obj.a()).dontworryaboutotherarguments.thenReturn(...); // choose a better name.
// or more of a structured looseness
when(obj.a()).andAnyOptionalArgs.thenReturn(...);
when(obj.a()).andAnyNamedArgs.thenReturn(...);
from mockito.
Seems like the toggle is a property of the 'when'-ness than of the Expectation. Perhaps when(someCall, {MatchMode matchArguments})
? Then it's semantically succinct and also discoverable.
from mockito.
I can see that.
when(obj.func(...), matchMode: MatchMode.ignoreArgs).thenReturn...
when(obj.func(...), matchMode: MatchMode.onlyRequiredArgs).thenReturn...
when(obj.func(...), matchMode: MatchMode.exactArgs).thenReturn... // default
WDYT @matanlurey @TedSander ?
from mockito.
SGTM
from mockito.
LG!
Just to understand the nuance between the first 2, given:
void a(B b, {C c, D d});
what would
when(obj.a(anInstanceOfB, c: anInstanceOfC), matchMode: MatchMode.ignoreArgs vs MatchMode.onlyRequiredArgs).thenReturn
do differently? Would MatchMode.ignoreArgs also match an actual invocation obj.a(null, null, null)
?. Would MatchMode.onlyRequiredArgs also match obj.a(b, null, null)
?
from mockito.
ignoreArgs
would match on anything, regardless of the anInstanceOfB
and anInstanceOfC
. onlyRequiredArgs
would require anInstanceOfB
to match, but not c
nor d
. If a call comes in that matches anInstanceOfB
, then the same ordering rules that Mockito uses today would apply.
ignoreArgs
would allow one to put in a bunch of null
s.
onlyRequiredArgs
would also match b, c: null, d: null
.
from mockito.
SG
from mockito.
I think ignoreArgs
and onlyRequiredArgs
are ambiguous. What if you have ignore
but then supply some args in the call? Should it ignore the ones you've supplied?
And onlyRequired
implies that positional args are required but keyword are not, but folks using meta's @required
would expect kwargs to be required too.
How about just 2 states, to start:
exact
, the default, which doesn't change any behaviorignoreExtra
which matches on supplied args but doesn't fail if additional args were passed to the call
(I removed theArgs
suffix because it felt redundant - perhaps the param and enum could be named[aA]rgsMode
instead.)
It would be really nice, too, to globally set the mode to ignoreExtra
, because we would do that in all of our projects.
from mockito.
This feature isn't going to be written, because it's largely been replaced by the noSuchMethod Forwarding feature of Dart 2.0.0-dev.56ish+ (see #122).
Basically, noSuchMethod will receive default values for optional (and optional named) parameters (of course, this happens during when
, real calls, and verify
), which solves @xster 's original request, and my HttpRequest.open example.
Mockito basically no longer has the power to see if an argument was passed or not (similar to how a method generally cannot tell if a default argument was used, or if an explicit argument, equal to the default value, was passed).
from mockito.
So when we upgrade to dart 2 the behavior will just default to be like ignoreExtra
?
from mockito.
Related Issues (20)
- Provide better documentation/examples for `provideDummy`/`provideDummyBuilder` HOT 3
- `thenAnswerInOrder` support
- Bump Analyzer version to >6 HOT 2
- Generated Imports use Unescaped backslashes HOT 3
- Verify constructor HOT 1
- How to test retrofit services with mockito HOT 6
- Question: dockito in dev_dependies? HOT 1
- Error The included part 'mocks.dart' must have a part-of directive. HOT 1
- `verify` failing test but showing in "All calls" HOT 1
- mockito:mockBuilder generator fail using MockSpec custom superclass HOT 5
- How to mock Dio.interceptors.add? HOT 1
- Support for extension-type/inline-class HOT 3
- Why is build_runner is succeeding with 0 outputs? HOT 3
- Support for real executions on Mocking classes HOT 1
- Bad state: Cannot call `when` within a stub response HOT 7
- Cannot create Mock in Dart 3.x: type 'Null' is not a subtype of type 'IOSink' HOT 1
- 'Null' is not a subtype of type 'Future<bool>' HOT 3
- Mocking a class which is created at runtime HOT 1
- Breaking change or typo? HOT 2
- How are we supposed to use any with non null parameters? HOT 2
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 mockito.