Skip to content

Instantly share code, notes, and snippets.

@escamoteur
Last active December 30, 2019 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save escamoteur/0e6815c72441b1895dec28ded924dd13 to your computer and use it in GitHub Desktop.
Save escamoteur/0e6815c72441b1895dec28ded924dd13 to your computer and use it in GitHub Desktop.

Hi, I have the problem that a Test that is used in the demo of rx_widgets longer works:

test('should filter after the user stops typing for 500ms', () async {
    // Use FakeAsync from the Quiver package to simulate time
    new FakeAsync().run((time) {
    final service = new MockService();
    final model = new HomePageModel(service);

    when(service.getWeatherEntriesForCity(any))
        .thenAnswer((_) => new Future.sync(() => <WeatherEntry>[]));

    model.textChangedCommand('A');
    time.elapse(new Duration(milliseconds: 1000));

    verify(service.getWeatherEntriesForCity('A'));
    });
});

I found out that the problem is that verify is called before service.getWeatherEntriesForCity is called with "A" which happens with a delay of 500ms because the HomePageModel uses rxdart's debounceTime()

// When the user starts typing
_textChangedCommand
    // Wait for the user to stop typing for 500ms
    .debounceTime( Duration(milliseconds: 500))
    // Then call the updateWeatherCommand
    .listen(_updateWeatherCommand);

I could see while debugging that service.getWeatherEntriesForCity('A') is indeed called but after verify.

If anynone wants to give it a shot I really would be grateful you can fine the repo here:

https://github.com/escamoteur/rx_widgets/blob/master/example/test/homepage_model_test.dart

@brianegan
Copy link

Yeh, looks like FakeAsync is causing troubles again. It's a shame, really -- the only solution I can think of is to remove FakeAsync and use regular awaits. The downside: The tests will take longer to run, but at least they work.

    test('should filter after the user stops typing for 500ms', () async {
      final service = MockService();
      final model = HomePageModel(service);

      when(service.getWeatherEntriesForCity('A'))
          .thenAnswer((_) => Future.sync(() => <WeatherEntry>[]));

      model.textChangedCommand('A');

      await Future.delayed(Duration(milliseconds: 750));

      verify(service.getWeatherEntriesForCity('A'));
    });

    test('should not search if the user has not paused for 500ms', () async {
      final service = MockService();
      final model = HomePageModel(service);

      when(service.getWeatherEntriesForCity(any))
          .thenAnswer((_) => Future.sync(() => <WeatherEntry>[]));

      model.textChangedCommand('A');

      await Future.delayed(Duration(milliseconds: 100));

      verifyNever(service.getWeatherEntriesForCity('A'));
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment