Flutter Unit Test, Two equal instances are failing

Viewed 833

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?

2 Answers

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

You can use by below method

  expect(loginViewmodel.loginResponseData.value, isA<BaseApiResponse<LoginResponse>>());

Here my loginResponseData is my response which is coming from the API response and isA<> is used for type matching so you can pass your response model

For more information please follow the below link Test Cases for API in Flutter

Related