lomsa-dev/http-mock-adapter

DioError [DioErrorType.other]: Converting object to an encodable object failed: Instance of 'Genre'

Closed this issue ยท 2 comments

I have a test written like this:

void main() {
  test("Function should return a Genre", () async {
    final Dio dio = Dio();
    final DioAdapter dioAdapter = DioAdapter();

    dio.httpClientAdapter = dioAdapter;

    dioAdapter.onGet('This is a test endpoint',
        (request) => request.reply(200, Genre('Romance', '/romance', 100, 50)));

    ApiClientProvider apiClientProvider = ApiClientProvider(dio);

    expect(
        await apiClientProvider.getGenreInfo(
            endpoint: 'This is a test endpoint'),
        isInstanceOf<Genre>());
  });
}

This is the getGenreInfo() method from ApiClientProvider class:

  Future<Genre> getGenreInfo({required String endpoint}) async {
    Response response = await _dio.get(endpoint);
    return response.data;
  }

I tried to mock the get() function to return the Genre('Romance', '/romance', 100, 50) but I end up getting DioError [DioErrorType.other]: Converting object to an encodable object failed: Instance of 'Genre'

Hello! First of all, thanks for opening the issue.

Are you sure, you're mocking the dio which you use in the getGenreInfo function?
I'm asking because in your test you've set a new dio and set dioAdapter to it. So probably you didn't mock dio which was used in the getGenreInfo function. However, the dio's httpClientAdapter isn't set, which you're trying to mock.

Please set the dio's (which you used in the getGenreInfo function.) httpClientAdapter.

Hello! First of all, thanks for opening the issue.

Are you sure, you're mocking the dio which you use in the getGenreInfo function?
I'm asking because in your test you've set a new dio and set dioAdapter to it. So probably you didn't mock dio which was used in the getGenreInfo function. However, the dio's httpClientAdapter isn't set, which you're trying to mock.

Please set the dio's (which you used in the getGenreInfo function.) httpClientAdapter.

Thank you for the response, yes I didn't mock the Dio because the documentation's example didn't mock the Dio either. I followed the documentation and end up getting an error.

I will try to mock the Dio