Flutter Unit Test with Wildcard parameters (Mockito)

June Ligan
2 min readFeb 8, 2021

Sometimes we have this scenario where we want to unit test the invocation of the service but unfortunately, the parameter is instantiated inside the method.

Example:

final _navigationService = locator<NavigationService>();// more code here
....
....
Future<void> goToTestView() async { Test result = await _navigationService.navigateTo(
Routes.testView,
arguments: new TestViewArguments(testArgs: 'Test!'));
.... // more code
....
}

So, The problem here is that you cannot mock the ‘TestViewArguments’ object because it is instantiated in the method. so the if you try to write a unit test something like this.

void main() {

group('TestViewModel test ...', () {
MockNavigationService navigationService;
TestViewModel model;
setUp(() {
model = TestViewModel();
navigationService = MockNavigationService(); locator.registerSingleton<NavigationService>(navigationService);
});
tearDown(() {
locator.unregister<NavigationService>();
});
// more unit tests
......
test('goToTestView should navigate ....', () async { // when:
model.goToTestView();
// then:
verify(navigationService.navigateTo(
Routes.testView
,
arguments: new TestViewArguments(testArgs: 'Test!'));
});

});
}
class MockNavigationService extends Mock implements NavigationService {}

The error will be something like this

No matching calls. All calls: MockNavigationService.navigateTo(/test-view, {arguments: Instance of 'TestViewArguments', id: null})

because the object you instantiated in the method is not equal to the object that you instantiated in the unit test.

https://images.app.goo.gl/HqR66LxgbwNRfz9y6

Sometimes this is a pain in the a** since this can be a blocker to your other tasks.

So, we will be using the Wildcard of Mockito flutter.
I’ll modify the sample unit test above to use the wildcard ‘anything’.

void main() {

group('TestViewModel test ...', () {
// setup/teardown
// more unit tests
......
test('goToTestView should navigate ....', () async {
verify(navigationService.navigateTo(
Routes.testView
, arguments: anything);
});

});
}
// mock services

There’s also another mockito feature where we can strictly check the arguments instead of using the ‘anything’ by using the
isInstanceOf<Object>()’.

void main() {

group('TestViewModel test ...', () {
// setup/teardown
// more unit tests
......
test('goToTestView should navigate ....', () async {
verify(navigationService.navigateTo(
Routes.testView
,
arguments: isInstanceOf<TestViewArguments>());
});

});
}
// mock services

So, this time if we change the value of the arguments the unit test should fail since we strictly check the arguments that it is an instance of TestViewArguments. (Disclaimer: I’m still figuring out how to check the parameters of the TestViewArguments :D but at least we are already closer to our goal. ;p oh yeah!

Here are the libraries I’m currently used.

Flutter Mockito reference:

I’m also a newbie, so if you have comments that can help me to grow, please do. Thanks :D

--

--