Comments (2)
In order to transition to strong mode, we have been converting our
when()
andverify()
calls from
any
andcaptureAny
totyped(any)
andtyped(captureAny)
However this doesn't work well with named parameters where
when(... foo: any) becomes when(... foo: typed(any, named: 'foo'))
This is all true
Why does typed need to know the name of the parameter?
You can look at the implementation to see the reasons the named
parameter is required. In brief, we can look at an example:
when(obj.fn(
typed(any), typed(any),
foo: typed(any, named: 'foo'), bar: typed(any: named('bar')));
// works like:
when // Declare that a "when" statement is in progress, and return a callable object.
( // Call the object returned by "when".
obj.fn( // Call noSuchMethod from Mock.
// Aha, it looks like we are passing an argument matcher to the call. Store the "any"
// value as the first argument to "fn".
typed(any),
// Aha, it looks like we are passing *another* argument matcher to the call. Store
// the "any" value as the *second* argument to "fn".
typed(any),
foo:
// It looks like we are passing *another* argument matcher to the call, but the
// order between this, and the "bar" call is not guaranteed. Dart might execute
// "foo:" first or "bar:" first. When "typed" is being executed, it of course
// cannot see that it's return value is being assigned to a named argument of a
// function, named "foo".
typed(any, named: 'foo')
bar:
// Same as above. Not sure which named parameter this is being assigned to, so
// must read the "named" argument of "typed".
typed(any, named: 'bar')
));
And the tests will break when the name of the parameter is refactored, which runs counter to one of the main advantages of Mockito compared to other mocking frameworks.
Also unfortunate, but in most cases the tests will throw reliably:
when(obj.fn(foo: typed(any))); // throws ArgumentError
when(obj.fn(typed(any, named: 'something'))); // throws ArgumentError
when(obj.fn(newFoo: typed(any, named: 'foo'))); // throws ArgumentError
The one case where Mockito will surprise and fail you is swapping named parameter names:
when(obj.fn(foo: typed(any, named: 'bar'), bar: typed(any, named: 'foo')));
Poor Mockito is sort of helpless here. You stored an argument named "foo" and one named "bar", and then sure enough, noSuchMethod received an Invocation with a named argument, "foo", and another, "bar".
Now that the build package is stabilizing and support is theoretically growing (CC @matanlurey), we're planning a code-generating release of Mockito, after Mockito 3, which will be able to analyze code, and generate mock methods in a smarter fashion than noSuchMethod.
from mockito.
Closed as WAI for now.
from mockito.
Related Issues (20)
- Can't run flutter test when I add mockito, due to test_api
- Freezed models can't be mocked without json_serializable HOT 8
- 5.4.2 incompatible with Freezed 2.4.2 HOT 1
- I configured build.yaml as this repo, but it's not working as I expected HOT 1
- Mocking the (AutoDispose)FamilyNotifier with code generation incorrect HOT 1
- 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 2
- 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
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.