mardi 3 septembre 2019

Testing the bloc state will pass even if some properties are not the same

This is mapEventToState of MyBloc:

@override
  Stream<MyBlocState> mapEventToState(MyBlocEvent event) async* {
    if (event is MyBlocInitialize) {
      yield MyBlocStateInitialized(event.something);
    }

where the states are defined like:

abstract class MyBlocState extends Equatable {
  EnterCredentialsState([List props = const []]) : super(props)
}

class MyBlocStateInitialized extends MyBlocState {
  final String _something;
  MyBlocStateInitialized(this._something);}

and the event like:

abstract class MyBlocEvent {}

class MyBlocEventInizialize extends MyBlocEvent{
  final string something;
  MyBlocEventInitialize(this.something);
}

Now, this is my test:

test('Should return MyBlocInitialized with a defined String', () {
    String _somethingString = 'Something';

expectLater(
_myBloc.state,emitsInOrder([
          MyBlocsStateUninitialized(),
          MyBlocStateInitialized(_somethingString)
        ]));

    _myBloc.dispatch(MyBlocEventInitialize(_somethingString);
}

The problem with this test is that it will just check if the bloc will yield MyBlocsStateUninitialized and MyBlocStateInitialized, but it will not check the string inside MyBlocStateInitialized. Actually I could also change in

expectLater(
_myBloc.state,emitsInOrder([
          MyBlocsStateUninitialized(),
          MyBlocInitialized('WRONG')
        ]));

    _myBloc.dispatch(MyBlocEventInitialize(_somethingString);
}

and it would still pass.

Aucun commentaire:

Enregistrer un commentaire