如何使用flutter_redux测试Flutter应用程序?

时间:2019-02-20 07:26:06

标签: testing redux flutter flutter-test flutter-redux

我试图为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)

如何正确模拟商店?

1 个答案:

答案 0 :(得分:0)

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

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