Flutter测试无尾错误错误:键入' null'类型的子类型不是Future< object?>'
我想测试当按钮10次时是否按下路线。当测试的小部件调用modular.to.pushnamed()
方法时,将以下错误丢弃了
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>
呼叫,将异常抛出agileamells
widget widget in
void goToResults() {
Modular.to.pushNamed(AgileSmellsResults.route, arguments: validatedSmells);
}
vardicatedsmells
是一个不可nulable list.Empty(growable:true)
我的测试是
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);
});
我模拟的ModularNavigate类,
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());
}
}
谢谢您帮助我
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过阅读“ noreferrer”>“ noreflow noreferrer”> oilbtail faq和pub.dev上的文档
这个问题是错误的问题。代码>推名方法。我更改了
参数:
by参数:any(ney(naty:'grauments')
在我的mockednavigator
和verify> verify < /代码>步骤
我的修复
MockedNavigator
Solved by reading the Mocktail FAQ and documentation on pub.dev
The problem was wrong stubbing of
pushNamed
method. I changedarguments: any
byarguments: any(named: 'arguments')
for correct stubbing in mymockedNavigator
and inverify
stepsMy fixed
mockedNavigator