I am writing unit tests for my flutter mobile application and I am trying to test a function that upon execution should pop the back stack until the predicate of the routing function is satisfied. Here is an example of the test code and accompanying navigation code:
group('popUntilHomeView - ', () {
test(
'when popUntilHomeView is called the application should pop the stack until reaching the home screen',
() {
// Arrange
final navigationService = getAndRegisterNavigationServiceMock();
final viewModel = CurrentViewModel();
// Act
viewModel.popUntilHomeView();
// Assert
verify(navigationService.popUntil((route) => route.settings.name ==
'/home-view')).called(1);
});
});
Here is the code from the NavigationService, which is built on the FilledStacked Architechure.
void popUntil(RoutePredicate predicate, {int? id}) {
G.Get.until(predicate, id: id);
}
Here is the result of running the above test:
No matching calls. All calls: MockNavigationService.popUntil(Closure: (Route) => bool, {id: null})
(If you called verify(...).called(0);
, please instead use verifyNever(...);
.)
I am using Mockito to mock the navigation service, here is the generated mock function:
@override
void popUntil(
_i8.RoutePredicate? predicate, {
int? id,
}) =>
super.noSuchMethod(
Invocation.method(
#popUntil,
[predicate],
{#id: id},
),
returnValueForMissingStub: null,
);
As you can see the function is getting called it is just not capturing the predicate I defined and instead returning a Closure.
Question
Does anyone have any ideas how to verify that the NavigationService popUntil is called with the proper predicate?
Notes:
- I have tried stubbing the function which provided some success but I can't do anything with the return value since the function is declared as void
- I have tried declaring the predicate as 'any' as an attempt to capture the invocation details but it will not allow since the predicate is not nullable.