Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 494
Little Monkey Avatar asked Oct 02 '18 06:10

Little Monkey


Video Answer


1 Answers

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

  • How does a set determine that two objects are equal in dart?
  • http://pchalin.blogspot.com/2014/04/defining-equality-and-hashcode-for-dart.html
  • https://api.dartlang.org/stable/2.0.0/dart-core/Object/hashCode.html
  • https://api.dartlang.org/stable/2.0.0/dart-core/String/operator_equals.html
like image 59
Günter Zöchbauer Avatar answered Sep 28 '22 06:09

Günter Zöchbauer