Little Monkey
Little Monkey

Reputation: 6157

Flutter: testing

I was trying to test this function

  UserApi createUserApi(String url, String username, String password) {
    UserApi userApi = new UserApi(base: route(url), serializers: repo);
    userApi.base.basicAuth('$username', '$password');
    return userApi;
  }

basically, the test was to compare the result of this function with a "manually composition" of it, expecting to have the same result. But It doesn't:

  String username = "asd";
  String password = "asd";
  UserApi userApiTest = new UserApi(base: route("asd"), serializers: repo);
  userApiTest.base.basicAuth('$username', '$password');
  test("UserApi creation", () {
    UserApi userApi = _presenter.createUserApi("asd", "asd", "asd");
    expect(userApi, userApiTest);
  }); 

The result is always :

Expected: <Instance of 'UserApi'>
  Actual: <Instance of 'UserApi'>

Why are they different? In the debug every property is the same.

Upvotes: 7

Views: 2426

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657178

You have two different instances of UserApi. Them having the same property values does not make them equal.

You would need to implement hashCode and operator==. By default only comparing the references to the same instance of an object are considered equal (because they are identical)

See also

Upvotes: 8

Related Questions