使用 mockito 进行测试时无法将数据添加到流中。当我调用流接收器函数时,它会在 null 上抛出函数调用。 auth.changeShowProgress(true) 返回函数调用 null。如何向 bloc 中的流添加数据?
testWidgets('Correct email and password gives success',
(WidgetTester widgetTester) async {
final AuthBloc auth = MockAuthBloc();
when(auth.showProgress).thenReturn(null);
when(auth.changeShowProgress(true)).thenAnswer((_) {
Future.value(null);
});
await widgetTester.pumpWidget(
makeTestableWidget(
authbloc: auth,
child: MaterialApp(home: LoginPage()),
),
);
await widgetTester.pump(Duration.zero);
verify(auth.showProgress).called(1);
expect(find.byType(CircularProgressIndicator), findsOneWidget);
await widgetTester.enterText(
find.bySemanticsLabel("Email address"), "admin@admin.com");
await widgetTester.enterText(find.bySemanticsLabel("Password"), "password");
await widgetTester.tap(find.widgetWithText(PRaisedButton, "Login"));
await widgetTester.pump();
});
最佳答案
您可能遇到不止一个问题。这段代码:
when(auth.changeShowProgress(true)).thenAnswer((_) {
Future.value(null);
});
应该是这样的:
when(auth.changeShowProgress(true)).thenAnswer((_) {
return Future.value(null);
});
或者像这样:
when(auth.changeShowProgress(true)).thenAnswer((_) => Future.value(null));
希望这对您有所帮助。
关于testing - Mockito:无法向流中添加数据;当我调用流接收器函数时,它会在 null 上抛出函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58534516/