testing - Mockito:无法向流中添加数据;当我调用流接收器函数时,它会在 null 上抛出函数调用

标签 testing flutter dart mockito

使用 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/

相关文章:

google-app-engine - 在Google App Engine VM中手动运行dart服务器

unit-testing - 编写测试时如何处理setUp()成瘾问题?

testing - Puppeteer Chromium,禁用 "Anonymize local IPs exposed by WebRTC"

dart - 如何在列表中的所有项目上调用函数?

ios - flutter 构建 xcode11.3 失败

android - 无法为 org.gradle.api.project 类型的项目 'android' 获取未知属性 ':shared_preferences'

android - 开发Native Plugin时 `GeneratedPluginRegistrant.class`的用途和用法

testing - 如何验证表中的列数?

testing - 基于 leiningen 测试元数据的 log4j 属性文件?

flutter - 基于AndroidViews实现的WebView(webview_flutter)如何截图?