我尝试为我的颤振应用程序编写第一个烟雾测试:

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)

如何正确地模仿商店?

最佳答案

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

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

10-08 05:39