简体   繁体   English

flutter 异常单元测试

[英]Unit testing for Exception in flutter

i'm trying to pass my unit test in flutter. i'm getting my expected value and actual value still my test got failed.我正在尝试通过 flutter 中的单元测试。我得到了我的期望值和实际值,但我的测试仍然失败了。

This is my Method in BaseApi class.这是我在 BaseApi class 中的方法。

Exception getNetErrorException(String url) => HttpException(url);异常 getNetErrorException(String url) => HttpException(url);

and this is my test:这是我的测试:

group("getNetErrorException", () {
  test("Should throw a HttpException when there is an error", () {
    BaseApi api = BaseApi();
    expect(api.getNetErrorException(""), HttpException(""));
  });
});

There's no way for expect to tell if one HttpException('') is "equal" to another HttpException('') unless HttpException overrides operator== , which it doesn't do. expect无法判断一个HttpException('')是否“等于”另一个HttpException('')除非HttpException覆盖operator== ,而它不会这样做。 The default operator == that it inherits from Object tests for object identity .它从Object继承的默认operator ==测试 object身份

You'd need to do:你需要做:

expect(api.getNetErrorException(''), isA<HttpException>());

or if you must test the error message, you could create a custom matcher to check it:或者如果您必须测试错误消息,您可以创建一个自定义匹配器来检查它:

expect(
  HttpException(''),
  allOf(
    isA<HttpException>(),
    predicate<HttpException>(
      (httpException) => httpException.message == ''),
  ),
);

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

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