为啥我的状态之一在我的测试期间没有被触发
Posted
技术标签:
【中文标题】为啥我的状态之一在我的测试期间没有被触发【英文标题】:Why one of my state is not triggered during my test为什么我的状态之一在我的测试期间没有被触发 【发布时间】:2020-08-22 02:16:38 【问题描述】:我跟随 Reso code youtube 创建了一个小型天气应用程序,但我也使用了最新的 bloc 库和 flutter_bloc 4.0。
基础应用:https://www.youtube.com/watch?v=hTExlt1nJZI&list=PLB6lc7nQ1n4jCBkrirvVGr5b8rC95VAQ5&index=7
BLoc 测试:https://www.youtube.com/watch?v=S6jFBiiP0Mc&list=PLB6lc7nQ1n4jCBkrirvVGr5b8rC95VAQ5&index=8
前 2 个测试正在运行。例如,下面的没有给我任何错误:
test(
'NEWER WAY BUT lONG-WINDED emits [WeatherLoading, WeatherLoaded] when successful',
()
when(mockWeatherRepository.fetchWeather(any))
.thenAnswer((_) async => weather);
final bloc = WeatherBloc(mockWeatherRepository);
bloc.add(GetWeather('London'));
emitsExactly(bloc, [
WeatherInitial(),
WeatherLoading(),
WeatherLoaded(weather),
]);
);
由于某种原因,下面的测试不会触发 WeatherInitial。
blocTest(
'emits [WeatherLoading, WeatherLoaded] when successful',
build: () async
when(mockWeatherRepository.fetchWeather(any))
.thenAnswer((_) async => weather);
return WeatherBloc(mockWeatherRepository);
,
act: (bloc) => bloc.add(GetWeather('London')),
expect: [
WeatherInitial(),
WeatherLoading(),
WeatherLoaded(weather),
],
);
错误是:
ERROR: Expected: [
WeatherInitial:WeatherInitial,
WeatherLoading:WeatherLoading,
WeatherLoaded:WeatherLoaded
]
Actual: [WeatherLoading:WeatherLoading, WeatherLoaded:WeatherLoaded]
Which: was WeatherLoading:<WeatherLoading> instead of WeatherInitial:<WeatherInitial> at location [0]
你知道为什么吗?
【问题讨论】:
【参考方案1】:根据官方bloc_test library这个行为是正确的:
skip is an optional int which can be used to skip any number of states.
The default value is 1 which skips the initialState of the bloc.
skip can be overridden to include the initialState by setting skip to 0.
因此,如果您只想包含“InitialState”,请将“skip”值设置为 0:
.
.
.
skip: 0,
expect: [
WeatherInitial(),
WeatherLoading(),
WeatherLoaded(weather),
],
【讨论】:
以上是关于为啥我的状态之一在我的测试期间没有被触发的主要内容,如果未能解决你的问题,请参考以下文章
为啥 cellForRowAtIndexPath 没有被触发,但其他方法被触发了?
为啥没有在我的 webView 中触发 observeValueForKeyPath?
我的 Redux 状态发生了变化,为啥 React 没有触发重新渲染?