title: Unit test - How to mock parameters of third-party-library class Dio in flutter
body: <p>I am trying to test a simple repository class which make a network call using the Dio package that is a dependency injection. Requirement of the Http.post is to send a Map object to a URL with the headers of <code>'Content-Type': 'application/json</code>. You can see this below: </p>
  
  <pre><code>class AuthenticateUserRemoteDataSourceImpl
      implements AuthenticateUserRepository {
    final Dio httpClient;
  
    AuthenticateUserRemoteDataSourceImpl({@required this.httpClient});
  
    @override
    Future&lt;Either&lt;Failure, AuthenticateUser&gt;&gt; getAuthenticateUser(
        String email, String password) async {
      final url = 'API_URL';
  
      final Map&lt;String, String&gt; jsonPayload = {
        "email": email,
        "password": password
      };
  
      Response response = await httpClient.post('{$url}api/users/authenticate',
          data: jsonPayload,
          options: Options(headers: {'Content-Type': 'application/json'}));
    }
  }
  </code></pre>
  
  <p>I am trying to make sure this method is covered by unit test, however I am having difficult verify the named parameters with the Dio package. I can verify that the <code>dioClient.post</code> is actually called however, I am having trouble with mocking the name parameters of 'data' and 'options'.</p>
  
  <p>I want to test that it gets called with <code>Map&lt;String, String&gt;</code> for the body, and also I would like to test the headers that are sent, I want to check that is it called with the <code>{'Content-Type': 'application/json'}</code>. This will be useful as well when I need to unit test Authenticated dio get/post/put requests.</p>
  
  <p>This is the test that I have so far, which as mentioned before I can verify that the mocked function is called, but not verifying the name params. </p>
  
  <pre><code>
    group('getAuthenticateUser', () {
      final tResponse = Response(statusCode: 200, data: {"title": "success"});
  
      test(
          'should perform a post request to the the API with the application/json header',
          () async {
        // arrange
        when(dioClient.post(any,
                data: anyNamed('data'), options: anyNamed('options')))
            .thenAnswer((Invocation invocation) async =&gt; tResponse);
  
        // act
        await dataSource.getAuthenticateUser(tEmail, tPassword);
  
        // assert
        verify(dioClient.post(any,
            data: anyNamed('data'), options: anyNamed('options')));
      });
    });
  </code></pre>
  
  <p>Thanks for your helps, I am just getting started with unit testing so any help or pointers will be great,</p>
  
  <p>Thanks, Sam</p>
  
  <p>@LoVe
  <a href="https://i.stack.imgur.com/nM021.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nM021.png" alt="enter image description here"></a></p>
  
  <p><em>Update</em></p>
  
  <p>I have implemented your mock class which worked great, and I think it was the thing that I was missing for sure. I have updated my test but cannot understand where I am going wrong now?</p>
  
  <p>test.dart</p>
  
  <pre><code>class MockOptions extends Mock implements Options {
    final Map&lt;String, dynamic&gt; headers;
    //also add any other parameters you want to mock as fields in this class
  
    MockOptions({this.headers});
  }
  
  
  test(
     'should perform a post request to the the API with the application/json header',
      () async {
          // arrange
          Map&lt;String, String&gt; headers = {'Content-type': 'application/json'};
  
          when(mockDio.post('path', options: anyNamed('options')))
            .thenAnswer((_) async =&gt; Response(data: {}));
  
          // act
          dataSource.getAuthenticateUser(tEmail, tPassword);
  
          // assert
          verify(mockDio.post('path', options: MockOptions(headers: headers)));
      });
  
  </code></pre>
  
  <p>and the method file looking like this: </p>
  
  <pre><code>  @override
    Future&lt;Either&lt;Failure, AuthenticateUser&gt;&gt; getAuthenticateUser(
        String email, String password) async {
  
      await this.httpClient.post('path',
          data: {},
          options: Options(headers: {'Content-type': 'application/json'}));
    }
  </code></pre>