Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to test fresh_dio with a Mock HttpClientAdapter? #94

Open
sidroniolima opened this issue Nov 7, 2023 · 0 comments
Open

How to test fresh_dio with a Mock HttpClientAdapter? #94

sidroniolima opened this issue Nov 7, 2023 · 0 comments

Comments

@sidroniolima
Copy link

Thanks for the package!

I'm trying to test an API that uses the fresh_dio passing a HttpClientAdapter Mock as a dio.httpClientAdapter.

I can make some tests pass. Like this one:

`final httpPostResponse = ResponseBody.fromString(
responseData,
200,
headers: {
Headers.contentTypeHeader: [Headers.jsonContentType],
},
);

    when(
      () => mockDioAdapter.fetch(
        any(
          that: isA<RequestOptions>().having(
            (options) => options.method,
            'POST method',
            equals('POST'),
          ),
        ),
        any(),
        any(),
      ),
    ).thenAnswer((_) async => httpPostResponse);

    await meuEspacoApiClient.authenticate(
      username: 'username',
      password: 'password',
    );

    expect(
      meuEspacoApiClient.authenticationStatus,
      emitsAnyOf([AuthenticationStatus.authenticated]),
    );

    verify(
      () => mockDioAdapter.fetch(
        any(
          that: isA<RequestOptions>()
              .having(
                (options) => options.method,
                'POST method',
                equals('POST'),
              )
              .having(
                (options) => options.baseUrl,
                'Base URL',
                equals('http://localhost:8080'),
              ),
        ),
        any(),
        any(),
      ),
    ).called(1);`

But when I return a response with a status code equals to 401, then a refresh function is not called and the following DioException occurs:

DioException [bad response]: The request returned an invalid status code of 401.

If I handle the exception it not trigger the refresh token even.

I'm using this package wrong or missing something?

Please, help me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant