skip to Main Content

I want to test equality between these two request objects but I can not figure out how to implement the hash and equality operator on a third-party package for example http package.
How can I use equatable package on the Response object of http package?

import 'package:http/http.dart' as http;
test('equality test', () async {
        final uri = baseUri.replace(path: 'account/login');
        final request = http.Request(RequestMethod.post, uri);
        final request2 = http.Request(RequestMethod.post, uri);
        expect(request, equals(request2));
});

The output is

Expected: Request:<POST https://test.webapi.odinpool.com:1989/account/login>
  Actual: Request:<POST https://test.webapi.odinpool.com:1989/account/login>

Which it doesn’t complete successfully.
I want to test the remote server API to call the correct API endpoint when creating the request object and test the method with verify.called(1) method but it seems the test package can not test equality between http.Request objects.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to @Richard Heap comment I came up with a solution:

    class RequestX extends http.Request with EquatableMixin{
      RequestX(super.method, super.url);
    
      @override
      List<Object?> get props => [super.headers, super.method, super.url, super.body];
    
    }
    

    This RequestX class compared two objects successfully.


  2. I think your best bet is going to be to either find/create a function to do a deep check.

    In reality though this is going to be a pretty rare need case. Either when you compare you are going to have a reference to the same object which will evaluate to true:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search