简体   繁体   English

颤振:测试

[英]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. 在调试中,每个属性都是相同的。

You have two different instances of UserApi . 您有两个不同的UserApi实例。 Them having the same property values does not make them equal. 它们具有相同的属性值并不会使它们相等。

You would need to implement hashCode and operator== . 您将需要实现hashCodeoperator== By default only comparing the references to the same instance of an object are considered equal (because they are identical) 默认情况下,仅将引用与对象的相同实例进行比较才视为相等(因为它们相同)

See also 也可以看看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM