Flutter Unit Test, Two equal instances are failing

474

You have two different instances of LoginModel. Even if they have same values, they are not equal.

Update your model with

class LoginModel{
 String name = "some";

  @override
  bool operator ==(Object other) => other is LoginModel && other.name == name;

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

or use

https://pub.dev/packages/equatable

Share:
474
Khaled Ramadan
Author by

Khaled Ramadan

Updated on December 29, 2022

Comments

  • Khaled Ramadan
    Khaled Ramadan over 1 year

    I'm expecting two instances to be equal.

    I wrote the code below:

      test('Should return a valid model when the user is fetched from the api',
          () async {
        final Map<String, dynamic> jsonMap =
            json.decode(fixture('login_fixture.json'));
        final result = LoginModel.fromJson(jsonMap);
        expect(result, testingLoginModel);
      });
    

    It's failed with the following messages:

      Expected: <Instance of 'LoginModel'>
        Actual: <Instance of 'LoginModel'>
      00:01 +0 -1: Some tests failed.
    

    as we can see the expected result is equal the actual one "<Instance of 'LoginModel'>"

    So why it is failing?