【问题标题】:Flutter test failure, unit test expected and actual state object are the same but still failFlutter 测试失败,单元测试预期和实际状态对象相同但仍然失败
【发布时间】:2021-07-29 12:24:12
【问题描述】:

我有一个这样的代码测试,预期和实际已经有相同的对象,但是单元测试仍然失败,我该怎么办?

 blocTest("_mapClickEmailToState",
        wait: const Duration(milliseconds: 500),
        build: () {
          return bloc;
        },
        act: (bloc) => bloc.add(ClickEmail()),
        expect: () => [
              ClickEmailSuccess()
        ]);
  });

我有这个错误

预期:['ClickEmailSuccess' 实例] 实际:[实例 'ClickEmailSuccess'] 其中:在位置 [0] 是 而不是

【问题讨论】:

  • 看来你需要覆盖ClickEmailSuccess 类的相等性。

标签: flutter unit-testing bloc


【解决方案1】:

您必须覆盖 == 运算符和 hashCode 或为该类实现 Equatable。

为了覆盖它们,让我们假设类 Person 具有属性 name 和 age,它看起来像这样:

class Person {
  const Person(this.name, this.age);

  final String name;
  final int age

  @override
  bool operator ==(Object other) =>
    identical(this, other) ||
    other is Person &&
    runtimeType == other.runtimeType &&
    name == other.name &&
    age == other.age;

  @override
  int get hashCode => name.hashCode ^ age.hashCode;
}

【讨论】:

  • 谢谢,这是在为我的班级实施 Equatable 后工作的
猜你喜欢
  • 1970-01-01
  • 2013-09-07
  • 1970-01-01
  • 2012-08-17
  • 2022-01-23
  • 1970-01-01
  • 2022-01-27
  • 2021-07-03
  • 2019-12-12
相关资源
最近更新 更多