7

我需要为 Flutter 项目编写单元测试,如果有一个函数可以遍历相同类型的两个不同对象的所有属性以确保所有值都相同,我会很高兴。

代码示例:

void main() {
  test('startLoadingQuizReducer sets isLoading true', () {
    var initState = QuizGameState(null, null, null, false);
    var expectedState = QuizGameState(null, null, null, true);

    var action = StartLoadingQuiz();
    var actualState = quizGameReducer(initState, action);
    // my test fails here 
    expect(actualState, expectedState);
  });
4

3 回答 3

4

如何覆盖==平等测试

这是一个覆盖==运算符的示例,以便您可以比较相同类型的两个对象。

class Person {
  final String name;
  final int age;
  
  const Person({this.name, this.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;
}

上面的例子来自这篇文章,建议你使用Equitable包来简化流程。这篇文章也值得一读。

于 2020-02-04T05:01:08.470 回答
0

您需要覆盖类中的相等运算符QuizGameState

于 2019-01-02T13:24:45.773 回答
0

添加到此处提供的答案,如果对象中有一个列表,您将不得不单独比较列表,如解释比较列表是否相等

于 2022-02-02T21:05:26.883 回答