即使某些属性不相同,测试 bloc 状态也会通过
Testing the bloc state will pass even if some properties are not the same
这是 MyBloc
的 mapEventToState
:
@override
Stream<MyBlocState> mapEventToState(MyBlocEvent event) async* {
if (event is MyBlocInitialize) {
yield MyBlocStateInitialized(event.something);
}
状态定义如下:
abstract class MyBlocState extends Equatable {
EnterCredentialsState([List props = const []]) : super(props)
}
class MyBlocStateInitialized extends MyBlocState {
final String _something;
MyBlocStateInitialized(this._something);}
事件如下:
abstract class MyBlocEvent {}
class MyBlocEventInizialize extends MyBlocEvent{
final string something;
MyBlocEventInitialize(this.something);
}
现在,这是我的测试:
test('Should return MyBlocInitialized with a defined String', () {
String _somethingString = 'Something';
expectLater(
_myBloc.state,emitsInOrder([
MyBlocsStateUninitialized(),
MyBlocStateInitialized(_somethingString)
]));
_myBloc.dispatch(MyBlocEventInitialize(_somethingString);
}
这个测试的问题在于它只会检查 bloc 是否会产生 MyBlocsStateUninitialized
和 MyBlocStateInitialized
,但不会检查 MyBlocStateInitialized
中的字符串。其实我也可以改变
expectLater(
_myBloc.state,emitsInOrder([
MyBlocsStateUninitialized(),
MyBlocInitialized('WRONG')
]));
_myBloc.dispatch(MyBlocEventInitialize(_somethingString);
}
它仍然会过去。
每次你在你的状态上有一个可比较的参数时,你必须将它传递给父构造函数 - Equatable
- 就像你对 MyBlocState
所做的那样,否则它将被初始化为一个空列表比较时你会收到误报。
来自 Equatable
The constructor takes an optional [List] of props
(properties) which will be used to determine whether two [Equatables] are equal.
If no properties are provided, props
will be initialized to an empty [List].
abstract class MyBlocState extends Equatable {
EnterCredentialsState([List props = const []]) : super(props)
}
class MyBlocStateInitialized extends MyBlocState {
MyBlocStateInitialized(this._something): super([_something]);
final String _something;
}
这是 MyBloc
的 mapEventToState
:
@override
Stream<MyBlocState> mapEventToState(MyBlocEvent event) async* {
if (event is MyBlocInitialize) {
yield MyBlocStateInitialized(event.something);
}
状态定义如下:
abstract class MyBlocState extends Equatable {
EnterCredentialsState([List props = const []]) : super(props)
}
class MyBlocStateInitialized extends MyBlocState {
final String _something;
MyBlocStateInitialized(this._something);}
事件如下:
abstract class MyBlocEvent {}
class MyBlocEventInizialize extends MyBlocEvent{
final string something;
MyBlocEventInitialize(this.something);
}
现在,这是我的测试:
test('Should return MyBlocInitialized with a defined String', () {
String _somethingString = 'Something';
expectLater(
_myBloc.state,emitsInOrder([
MyBlocsStateUninitialized(),
MyBlocStateInitialized(_somethingString)
]));
_myBloc.dispatch(MyBlocEventInitialize(_somethingString);
}
这个测试的问题在于它只会检查 bloc 是否会产生 MyBlocsStateUninitialized
和 MyBlocStateInitialized
,但不会检查 MyBlocStateInitialized
中的字符串。其实我也可以改变
expectLater(
_myBloc.state,emitsInOrder([
MyBlocsStateUninitialized(),
MyBlocInitialized('WRONG')
]));
_myBloc.dispatch(MyBlocEventInitialize(_somethingString);
}
它仍然会过去。
每次你在你的状态上有一个可比较的参数时,你必须将它传递给父构造函数 - Equatable
- 就像你对 MyBlocState
所做的那样,否则它将被初始化为一个空列表比较时你会收到误报。
来自 Equatable
The constructor takes an optional [List] of
props
(properties) which will be used to determine whether two [Equatables] are equal. If no properties are provided,props
will be initialized to an empty [List].
abstract class MyBlocState extends Equatable {
EnterCredentialsState([List props = const []]) : super(props)
}
class MyBlocStateInitialized extends MyBlocState {
MyBlocStateInitialized(this._something): super([_something]);
final String _something;
}