gpt4 book ai didi

即使某些属性不相同,测试 bloc 状态也会通过

转载 作者:行者123 更新时间:2023-11-28 21:16:16 25 4
gpt4 key购买 nike

这是 MyBlocmapEventToState:

@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 是否会产生 MyBlocsStateUninitializedMyBlocStateInitialized,但不会检查 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;
}

关于即使某些属性不相同,测试 bloc 状态也会通过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57772406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com