nonybrighto
nonybrighto

Reputation: 9593

TimeoutException in test using BehaviorSubject - BLoC

I created a simple Bloc that makes use of BehaviorSubject and I'm trying to test its emitted values, but I keep getting TimeoutException during the test or error in order when I swap the streams added.

The bloc

class ApplicationBloc extends BlocBase{

  final _appTitle = BehaviorSubject<String>();
  Function(String)  get changeTitle => (title) => _appTitle.sink.add(title);
  Stream<String> get apptitle => _appTitle.stream;

  ApplicationBloc(){
   // _appTitle.sink.add('title');
  }
  @override
  void dispose() {
    _appTitle.close();
  }
}   

The Test

 test('check title correct', (){

        //works
       /* appBloc.changeTitle('hi');
        expect(appBloc.apptitle, emitsInAnyOrder(['hi']));*/

        //doesn't work
        appBloc.changeTitle('hi');
        appBloc.changeTitle('hello');
        expect(appBloc.apptitle, emitsInOrder(['hi', 'hello']));
  });  

When the title stream emits a single item, it works okay. But when it emits multiple items, it times out. This is the error I get when the emittion order is swapped

ERROR: Expected: should do the following in order: * emit an event that 'hi' * emit an event that 'hello' Actual: '> Which: emitted * hello which didn't emit an event that 'hi' because it emitted an event that is different. Expected: hi Actual: hello ^ Differ at offset 1

NOTE: Everything works as I expect when I change the BehaviorSubject to StreamController

Upvotes: 2

Views: 378

Answers (1)

Benoit Jadinon
Benoit Jadinon

Reputation: 1161

the timeout part may have been a bug, cause today with RxDart 0.24.1, there is no timeout anymore.

but the test doesn't pass still, because BehaviorSubject is only returning the latest value when expect subscribes to .apptitle to listen for values.

for a subject to return everything it was given, use a ReplaySubject.

Upvotes: 0

Related Questions