[英]Bad state: Cannot emit new states after calling close
I have a problem in Test section of flutter, I wrote a test for simple counter app which uses cubit, but while I run all test it gives me the error mentioned in header, any body knows why?我在 flutter 的测试部分遇到问题,我为使用 cubit 的简单计数器应用程序编写了一个测试,但是当我运行所有测试时,它给了我 header 中提到的错误,有人知道为什么吗?
it is necessary to say that while I run the tests one by one all runs successfully, but when I run all group it returns that error in second and third test...有必要说,虽然我一一运行测试都成功运行,但是当我运行所有组时,它会在第二次和第三次测试中返回该错误......
these are mu code这些是mu代码
group("Counter Cubit", () {
CounterCubit counterCubit = CounterCubit();
setUp(() {
//counterCubit = CounterCubit();
});
tearDown(() {
counterCubit.close();
});
test("The initial state for counterCubit is CounterState(counterValue: 0)",
() {
expect(counterCubit.state, CounterState(0, false));
});
blocTest(
"The cubit should emit CounterState(counter: 1, isIncrement: true) while we call
counterCubit.increment() ",
build: () => counterCubit,
act: (CounterCubit cubit) => cubit.increment(),
expect: () => [CounterState(1, true)]);
blocTest(
"The cubit should emit CounterState(counter: -1, isIncrement: false) while we call
counterCubit.decrement() ",
build: () => counterCubit,
act: (CounterCubit cubit) => cubit.decrement(),
expect: () => [CounterState(-1, false)]);
});
and my cubit and state are like below:我的cubit和state如下:
class CounterCubit extends Cubit<CounterState> {
CounterCubit() : super(CounterState(0, false));
void increment() => emit(CounterState(state.counter + 1, true));
void decrement() => emit(CounterState(state.counter - 1, false));
}
and state like:和 state 一样:
class CounterState extends Equatable {
final int counter;
final bool isIncremented;
CounterState(this.counter, this.isIncremented);
@override
List<Object?> get props => [counter, isIncremented];
}
bloc_test
documentation says: bloc_test
文档说:
blocTest creates a new
bloc
-specific test case with the givendescription
.blocTest使用给定的
description
创建一个新的bloc
特定测试用例。 blocTest will handle asserting that the bloc emits the expected states (in order) afteract
is executed.blocTest 将在执行
act
后处理断言 bloc 发出预期状态(按顺序)。blocTest
also handles ensuring that no additional states are emitted by closing the bloc stream before evaluating theexpect
ation.blocTest
还通过在评估期望值之前关闭 bloc stream 来expect
不会发出其他状态。
So basically when you run:所以基本上当你运行时:
blocTest(
// ...
build: () => counterCubit, // Same instance for all tests
// ...
);
It disposes your BLoC instance (that you pass through build
parameter).它处理您的 BLoC 实例(您通过
build
参数传递)。 So since the second test is using the same instance of the first test it throws an exception because in truth it was closed by the last blocTest
call (in the previous test).因此,由于第二个测试使用的是第一个测试的相同实例,因此它会引发异常,因为实际上它已被最后一次
blocTest
调用(在上一个测试中)关闭。
And it also answer why running tests one by one works but not the group.它还回答了为什么逐个运行测试有效,但对组无效。
To fix pass a new instance when running blocTest
(through the same parameter):要修复在运行
blocTest
时传递一个新实例(通过相同的参数):
blocTest(
// ...
build: () => CounterCubit(), // Create a new instance
// ...
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.