How do you test StreamBuilder in Flutter using Mockito?

Amac

I'm trying to test a widget that contains a StreamBuilder and I'm unable to return data.

Widget:

class HomeView extends StatelessWidget {
  final EventServiceRepository eventService;

  HomeView({this.eventService});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: StreamBuilder<List<Event>>(
         stream: this.eventService.getEvents(dates: dates),
         builder: (context, snapshot) {
           if (!snapshot.hasData) return HomeLoading();
           return EventsView(events: snapshot.data);
         },
       )
    );
  }
}

Test:

class MockedEventService extends Mock implements EventServiceRepository {}
class MockedEventStream extends Mock implements Stream<List<Event>> {}

testWidgets('Should find EventsView', (WidgetTester tester) async {
    final mockedEventService = MockedEventService();
    final mockedEventStream = MockedEventStream();
    when(mockedEventService.getEvents(dates: ['1/1/2021']))
        .thenAnswer((_) => mockedEventStream);

    when(mockedEventStream); // return list of events?

    await tester.pumpWidget(HomeView(eventService: mockedEventService));

    expect(find.byType(EventsView), findsOneWidget);
});

I've tried a few things with mockedEventStream but It always returns snapshot.hasData == false

Nick Fisher

Two issues - first, you're mocking the Stream, but you're not putting any data on it, so the StreamBuilder assumes the Stream is empty. Second, once you do put data on the Stream, you need to pump the Widget a second time to refresh the StreamBuilder. I also personally wouldn't bother mocking the Stream itself.

My slightly revised version:

final mockedEventService = MockedEventService();
when(mockedEventService.getEvents()).thenAnswer((_) => Stream.value(true));
await tester.pumpWidget(MyHomePage(eventService: mockedEventService)); 
await tester.pump(Duration.zero); 
expect(find.byKey(ObjectKey("B")), findsOneWidget);

Obviously need to change to reflect your original code (mock EventServiceRepository, return a Stream.value(List[]), check for the right Widget, etc etc).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do you mock classes that are used in a service that you're trying to unit test using JUnit + Mockito

From Dev

How do you test a service in spring mvc with mockito, rxjava2 using an io scheduler?

From Dev

How to do integ test using mockito

From Dev

How do you test if an exception is thrown in Flutter?

From Dev

How to test nulls using mockito?

From Dev

How do you specify test suites in Intern using a wildcard?

From Dev

How do you print in a Go test using the "testing" package?

From Dev

How to test DAO methods using Mockito?

From Dev

How to unit test flink timers using mockito

From Dev

How to test springjdbcTemplate using junit and mockito

From Java

Flutter: How do you make a card clickable?

From Dev

How do I simplify mockito/hamcrest argument matchers in test method?

From Dev

How do you test a Gmail Contextual Gadget?

From Java

How do you unit test private methods?

From Dev

How do you test for NaN in JavaScript?

From Dev

How do you test a mutable, implied result?

From Java

How do you test for a specific Rust error?

From Java

How do you skip a unit test in Django?

From Dev

How do you test an enum flag combination?

From Dev

How do you test your database design?

From Dev

How do you test the identity of Strings in Swift?

From Dev

How do you inject a repository into a test?

From Dev

How do you Bulk Test URL Redirections?

From Dev

How do you test functions that return Result?

From Dev

How do you test certificate pinning with Alamofire?

From Dev

How do you Bulk Test URL Redirections?

From Dev

How do you test the value of charAt(0)?

From Dev

How do you achieve a stress test in Gatling?

From Dev

How do you use test-unit?

Related Related

HotTag

Archive