1

i'm trying to pass my unit test in flutter. i'm getting my expected value and actual value still my test got failed.

This is my Method in BaseApi class.

Exception 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(""));
  });
});
2
  • 1
    Can i see the exact error? Commented Feb 28, 2022 at 12:03
  • @rapaterno getNetErrorException test/network/api/impl/BaseApi_test.dart Testing started at 5:24 PM ... Expected: _Exception:<Exception> Actual: _Exception:<Exception> package:test_api expect package:flutter_test/src/widget_tester.dart 461:3 expect test/network/api/impl/BaseApi_test.dart 63:9 main.<fn>.<fn>.<fn> it seems i'm comparing 2 different objects may be that's y i'm getting error but i don't know the solution Commented Feb 28, 2022 at 12:42

1 Answer 1

1

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. The default operator == that it inherits from Object tests for object identity.

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 == ''),
  ),
);
Sign up to request clarification or add additional context in comments.

1 Comment

Wonderful !! your test is passed 😄

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.