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

Viewed 975

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'>

1 Answers

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;
}
Related