Flutter test failure, unit test expected and actual state object are the same but still fail

625

you must override the == operator and hashCode or implement Equatable for that class.

To override them let's assume the class Person with the attributes name and age, it would look something like this:

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;
}
Share:
625
Rohmatul Laily
Author by

Rohmatul Laily

Updated on December 29, 2022

Comments

  • Rohmatul Laily
    Rohmatul Laily 10 months

    I have a code test like this, the expected and the actual already have the same object, but the unit test still fails, what should I do?

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

    i have this error

    Expected: [Instance of 'ClickEmailSuccess'] Actual: [Instance of 'ClickEmailSuccess'] Which: at location [0] is <Instance of 'ClickEmailSuccess'> instead of <Instance of 'ClickEmailSuccess'>

    • Jigar Patel
      Jigar Patel over 2 years
      Seems like you need to override equality for ClickEmailSuccess class.
  • Rohmatul Laily
    Rohmatul Laily over 2 years
    thank you, this is worked after implement Equatable for my class