title: How to mock BLoC in Integration Test
body: <p>I have a Flutter app that uses the <code>bloc</code> and <code>flutter_bloc</code> packages. I am trying to write an integration test using <code>flutter_driver</code> and <code>mockito</code>. How do I mock the <code>BlocProvider</code> and its children to always emit a single mocked state?</p>
  
  <p>The original bloc:</p>
  
  <pre class="lang-dart prettyprint-override"><code>class SummaryBloc extends Bloc&lt;BlocEvent, BlocState&gt; {
    final SummaryRepository repository;
  
    SummaryBloc(this.repository);
  
    @override
    BlocState&lt;Summary&gt; get initialState =&gt; BlocStateEmpty&lt;Summary&gt;();
  
    @override
    Stream&lt;BlocState&gt; mapEventToState(
      BlocEvent event,
    ) async* {
      if (event is Fetch) yield BlocStateSuccess&lt;Summary&gt;(repository.getSummary());
    }
  }
  </code></pre>
  
  <p>I have tried mocking the bloc and overriding the <code>state</code> getter:</p>
  
  <pre class="lang-dart prettyprint-override"><code>class MockSummaryBloc extends Mock implements SummaryBloc {}
  </code></pre>
  
  <p>Then using mockito in the instrumented app:</p>
  
  <pre class="lang-dart prettyprint-override"><code>final MockSummaryBloc summaryBloc = MockSummaryBloc();
  
  // whenever the state stream getter is called, return the mocked stream.
  when(summaryBloc.state).thenAnswer((_) async* {
      yield BlocStateSuccess&lt;Summary&gt;(mockSummary);
    });
  </code></pre>
  
  <p>For good measure I have tried the same for <code>initialState</code>.</p>
  
  <p>To use the mock in the instrumented app, I use <code>BlocProvider.value</code>:</p>
  
  <pre class="lang-dart prettyprint-override"><code>runApp(BlocProvider.value(
      value: summaryBloc,
      child: MaterialApp(
        title: 'Test Driver',
        home: Scaffold(
            body: BlocBuilder(
          bloc: summaryBloc,
          // the builder rebuilds on state change.
          builder: (context, state) {
            return Text('state: ${state?.runtimeType ?? 'isNull'}');
          },
        )),
      ),
    ));
  </code></pre>
  
  <p>I expect the Text widget to print <code>BlocStateSuccess&lt;Summary&gt;</code> during runtime. However, the state is null during the lifespan of the driver test.</p>
  
  <p>Am I incorrectly mocking this Bloc? Are there other methods of mocking a Bloc, or <code>Stream&lt;BlocState&gt;</code>?</p>