Flutter blocTest 返回空 'actual' 数组
Flutter blocTest returning empty 'actual' array
所以我有一个简单的 Cubit class,看起来像这样:
根据列表的 scrollValue 更改 isClosed 值是必要的,因此如果用户向上滚动,一些小部件将关闭。
const SCROLL_CLOSE_VALUE = 50;
class CloseContainerCubit extends Cubit<CloseContainerState> {
CloseContainerCubit()
: super(CloseContainerState(
isClosed: false,
scrollValue: .0,
));
void updateCloseContainer(scroll) =>
emit(CloseContainerState(isClosed: state.isClosed, scrollValue: scroll));
void handleClosingContainer() {
bool isClosedEmitValue = state.scrollValue > SCROLL_CLOSE_VALUE;
emit(CloseContainerState(
isClosed: isClosedEmitValue, scrollValue: state.scrollValue));
}
}
状态 class 看起来像这样:
class CloseContainerState extends Equatable {
final bool isClosed;
final double scrollValue;
CloseContainerState({required this.isClosed, required this.scrollValue});
@override
List<Object?> get props => [isClosed, scrollValue];
}
并尝试为其创建测试:
void main() {
group('CloseContainerCubit', () {
late CloseContainerCubit cubit;
setUp(() {
cubit = CloseContainerCubit();
});
tearDown(() {
cubit.close();
});
test(
'the initial state for the CloseContainerCubit is that cubit is not closed and scrollValue is 0)',
() {
expect(
cubit.state, CloseContainerState(isClosed: false, scrollValue: .0));
});
blocTest<CloseContainerCubit, CloseContainerState>(
'the cubit should emit a CloseContainerCubit(isClosed: false) '
'when cubit.updateCloseContainer(scroll) is called'
'and scroll < SCROLL_CLOSE_VALUE',
build: () => cubit,
act: (bloc) {
bloc.updateCloseContainer(SCROLL_CLOSE_VALUE - 1);
bloc.handleClosingContainer();
},
expect: () => <CloseContainerState>[
CloseContainerState(
isClosed: false, scrollValue: cubit.state.scrollValue)
],
);
});
}
第一个测试通过得很好,但是“blocTest”给我一个错误,“实际”是 []:
package:test_api expect
package:bloc_test/src/bloc_test.dart 193:9 testBloc.<fn>
===== asynchronous gap ===========================
dart:async _asyncThenWrapperHelper
package:bloc_test/src/bloc_test.dart testBloc.<fn>
dart:async runZonedGuarded
package:bloc_test/src/bloc_test.dart 172:9 testBloc
package:bloc_test/src/bloc_test.dart 140:11 blocTest.<fn>
package:bloc_test/src/bloc_test.dart 139:26 blocTest.<fn>
Expected: [CloseContainerState:CloseContainerState(false, 0.0)]
Actual: []
Which: at location [0] is [] which shorter than expected
我做错了什么?为什么它总是 returns 一个空数组而不是一些状态?
您的 updateCloseContainer 函数中缺少滚动参数的类型。
void updateCloseContainer(double scroll) {
emit(CloseContainerState(isClosed: state.isClosed, scrollValue: scroll));
}
所以我有一个简单的 Cubit class,看起来像这样: 根据列表的 scrollValue 更改 isClosed 值是必要的,因此如果用户向上滚动,一些小部件将关闭。
const SCROLL_CLOSE_VALUE = 50;
class CloseContainerCubit extends Cubit<CloseContainerState> {
CloseContainerCubit()
: super(CloseContainerState(
isClosed: false,
scrollValue: .0,
));
void updateCloseContainer(scroll) =>
emit(CloseContainerState(isClosed: state.isClosed, scrollValue: scroll));
void handleClosingContainer() {
bool isClosedEmitValue = state.scrollValue > SCROLL_CLOSE_VALUE;
emit(CloseContainerState(
isClosed: isClosedEmitValue, scrollValue: state.scrollValue));
}
}
状态 class 看起来像这样:
class CloseContainerState extends Equatable {
final bool isClosed;
final double scrollValue;
CloseContainerState({required this.isClosed, required this.scrollValue});
@override
List<Object?> get props => [isClosed, scrollValue];
}
并尝试为其创建测试:
void main() {
group('CloseContainerCubit', () {
late CloseContainerCubit cubit;
setUp(() {
cubit = CloseContainerCubit();
});
tearDown(() {
cubit.close();
});
test(
'the initial state for the CloseContainerCubit is that cubit is not closed and scrollValue is 0)',
() {
expect(
cubit.state, CloseContainerState(isClosed: false, scrollValue: .0));
});
blocTest<CloseContainerCubit, CloseContainerState>(
'the cubit should emit a CloseContainerCubit(isClosed: false) '
'when cubit.updateCloseContainer(scroll) is called'
'and scroll < SCROLL_CLOSE_VALUE',
build: () => cubit,
act: (bloc) {
bloc.updateCloseContainer(SCROLL_CLOSE_VALUE - 1);
bloc.handleClosingContainer();
},
expect: () => <CloseContainerState>[
CloseContainerState(
isClosed: false, scrollValue: cubit.state.scrollValue)
],
);
});
}
第一个测试通过得很好,但是“blocTest”给我一个错误,“实际”是 []:
package:test_api expect
package:bloc_test/src/bloc_test.dart 193:9 testBloc.<fn>
===== asynchronous gap ===========================
dart:async _asyncThenWrapperHelper
package:bloc_test/src/bloc_test.dart testBloc.<fn>
dart:async runZonedGuarded
package:bloc_test/src/bloc_test.dart 172:9 testBloc
package:bloc_test/src/bloc_test.dart 140:11 blocTest.<fn>
package:bloc_test/src/bloc_test.dart 139:26 blocTest.<fn>
Expected: [CloseContainerState:CloseContainerState(false, 0.0)]
Actual: []
Which: at location [0] is [] which shorter than expected
我做错了什么?为什么它总是 returns 一个空数组而不是一些状态?
您的 updateCloseContainer 函数中缺少滚动参数的类型。
void updateCloseContainer(double scroll) {
emit(CloseContainerState(isClosed: state.isClosed, scrollValue: scroll));
}