Reputation: 31
I want to test if a route is pushed when a button is tapped 10 times. When the Modular.to.pushNamed()
method is called by the tested widget, the following error was thrown
The following _TypeError was thrown running a test:
type 'Null' is not a subtype of type 'Future<Object?>'
When the exception was thrown, this was the stack:
#0 ModularNavigateMock.pushNamed (package:flutter_modular/src/presenter/models/modular_navigator.dart:40:14)
#1 AgileSmellsState.goToResults (package:serious_agile_games/agile-smells/agile_smells.dart:37:16)
#2 AgileSmellsState.build.<anonymous closure> (package:serious_agile_games/agile-smells/agile_smells.dart:50:26)
#3 SwipingDeck.swipeRight (package:swiping_card_deck/swiping_card_deck.dart:119:38)
<asynchronous suspension>
The call throwing the exception in AgileAmells
widget is
void goToResults() {
Modular.to.pushNamed(AgileSmellsResults.route, arguments: validatedSmells);
}
Where validatedSmells
is a not nullable List.empty(growable: true)
My test is
testWidgets('Test validate smell 10 times', (tester) async {
await tester.pumpWidget(TestModularApp(widgetToTest: AgileSmells(firestore: firestore)));
await tester.pumpAndSettle();
final deck = find.byType(SwipingDeck<SmellCard>);
final validateButton = find.byIcon(Icons.check);
expect(deck, findsOneWidget);
expect(validateButton, findsOneWidget);
await tester.tap(validateButton);
await tester.pumpAndSettle();
verifyNever(
() => TestModularNavigation.mockedNavigator.pushNamed(AgileSmellsResults.route, arguments: any, forRoot: false),
);
for (int i = 1; i < 10; i++) {
await tester.tap(validateButton);
await tester.pumpAndSettle();
}
// Verify causing failure in the test
verify(
() => TestModularNavigation.mockedNavigator.pushNamed(AgileSmellsResults.route, arguments: any, forRoot: false),
).called(1);
});
My mocked ModularNavigate class
class ModularNavigateMock extends Mock implements IModularNavigator {}
class TestModularNavigation {
static final mockedNavigator = ModularNavigateMock();
static void setUp() {
Modular.navigatorDelegate = mockedNavigator;
when(() => mockedNavigator.pushNamed(AgileSmellsResults.route, arguments: any)).thenAnswer((_) async => const AgileSmellsResults());
}
}
Thank you for helping me
Upvotes: 1
Views: 1192
Reputation: 31
Solved by reading the Mocktail FAQ and documentation on pub.dev
The problem was wrong stubbing of pushNamed
method. I changed arguments: any
by arguments: any(named: 'arguments')
for correct stubbing in my mockedNavigator
and in verify
steps
My fixed mockedNavigator
class ModularNavigateMock extends Mock implements IModularNavigator {}
class TestModularNavigation {
static final mockedNavigator = ModularNavigateMock();
static void setUp() {
Modular.navigatorDelegate = mockedNavigator;
when(() => mockedNavigator.pushNamed(AgileSmellsResults.route, arguments: any(named: 'arguments'))).thenAnswer((_) async => const AgileSmellsResults(validatedSmellCards: []));
}
}
Upvotes: 2