简体   繁体   English

坏 state:调用关闭后无法发出新状态

[英]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 given description . blocTest使用给定的description创建一个新的bloc特定测试用例。 blocTest will handle asserting that the bloc emits the expected states (in order) after act is executed. blocTest 将在执行act后处理断言 bloc 发出预期状态(按顺序)。 blocTest also handles ensuring that no additional states are emitted by closing the bloc stream before evaluating the expect 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.

相关问题 StateError(错误状态:调用关闭后无法添加新事件) - StateError (Bad state: Cannot add new events after calling close) 错误:错误状态:调用关闭后无法添加新事件 - Error: Bad state: Cannot add new events after calling close 错误 State:调用关闭后无法添加新事件 - Flutter 模块化 - Bad State: Cannot add new events after calling close - Flutter Modular flutter:未处理的异常:错误 state:调用关闭后无法添加新事件 - flutter: Unhandled Exception: Bad state: Cannot add new events after calling close 未处理的异常:错误 state:调用关闭音频播放器后无法添加新事件 - Unhandled Exception: Bad state: Cannot add new events after calling close Audio player Flutter:未处理的异常:错误状态:调用close后无法添加新事件(不一样的情况) - Flutter: Unhandled Exception: Bad state: Cannot add new events after calling close (NOT SAME CASE) Flutter:来自后退导航的回调产生“错误的 state:调用关闭后无法添加新事件” - Flutter : Callback from back navigation produces “Bad state: Cannot add new events after calling close” 未处理的异常:错误 state:调用关闭后无法添加新事件? - Unhandled Exception: Bad state: Cannot add new events after calling close? 未处理的异常:错误 state:在使用 fluttter_google_places 调用关闭后无法添加新事件 - Unhandled Exception: Bad state: Cannot add new events after calling close, on using fluttter_google_places 错误状态:调用来自 FirebaseAuth.veryfyPhone 的 phoneCodeAutoRetrievalTimeout 回调后无法添加新事件。 弗特 - Bad state: Cannot add new events after phoneCodeAutoRetrievalTimeout callback from FirebaseAuth.veryfyPhone gets called. Futter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM