首页 > 解决方案 > 如何使用 flutter_redux 测试 Flutter 应用程序?

问题描述

我尝试为我的 Flutter 应用程序编写第一个冒烟测试:

class MockStore extends Mock implements Store<AppState> {}

void main() {
  MockStore mockStore;
  setUp(() {
    mockStore = MockStore();
    when(mockStore.state).thenReturn(AppState.initial());
  });

  testWidgets('Login screen smoke test', (WidgetTester tester) async {
    // Build our app and trigger a frame.
    await tester.pumpWidget(App(mockStore,));

    // Verify that title is shown.
    expect(find.text('Sign in with google'), findsOneWidget);
  });
}

但我得到:

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building StoreConnector<AppState, _ViewModel>:
The method 'map' was called on null.
Receiver: null
Tried calling: map<_ViewModel>(Closure: (AppState) => _ViewModel)

如何正确模拟商店?

标签: testingreduxflutterflutter-testflutter-redux

解决方案


您的模拟需要为 onChange getter 返回一些东西,例如:

when(mockStore.onChange).thenAnswer((_) =>
    Stream.fromIterable([AppState.intial()]));

推荐阅读