Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

http-mock-adapter Dio Assertion failed: "Could not find mocked route matching request for

I'm trying to mock my Dio client with http-mock-adapter on put method and error method but I get this errors:

Exception: Assertion failed: "Could not find mocked route matching request for POST /onboard/answer { data: {"pageId":"1"},

Exception: DioError [DioErrorType.response]: {message: Some beautiful error!}

I found this issue https://githubmemory.com/repo/lomsa-dev/http-mock-adapter/issues/96?page=2 but I didn't solve my problem. Below is the code

main() {
  final dio = Dio(BaseOptions());
  final dioAdapter = DioAdapter(dio: dio);
  dio.httpClientAdapter = dioAdapter;

  final service = NetworkService(dio);

  test("should return a DioError", () async {
    const path = "/onboard/page-date/0/lastAnswerId";
    final dioError = DioError(
      error: {'message': 'Some beautiful error!'},
      requestOptions: RequestOptions(path: path),
      response: Response(
        statusCode: 404,
        requestOptions: RequestOptions(path: path),
      ),
      type: DioErrorType.response,
    );
    dioAdapter.onGet(path, (server) {
      server.throws(404, dioError);
    });
    final result = await service.getOnboardingAnswer("lastAnswerId");
    expect(result, isA<OnboardModel>());
  });


  final answerModel = AnswerModel(pageId: "1");

  test("should return 200", () async {
    const path = "/onboard/answer";
    dioAdapter.onPost(path, (server) {
      server.reply(200, 200);
    });
    final result = await service.postOnboardingAnswer(answerModel);
    expect(result, isA<int>());
  });
like image 433
Henrique Tavolaro Avatar asked Oct 24 '25 03:10

Henrique Tavolaro


1 Answers

instead of final dioAdapter = DioAdapter(dio: dio);

use final dioAdapter = DioAdapter(dio: dio, matcher: const UrlRequestMatcher()); this line only match passed URL not entire http request, so it should work on your case

in this example from official lib your default adapter using FullHttpRequestMatcher which means that it will match your entire request including passed POST parameter

like image 181
Pif Avatar answered Oct 25 '25 18:10

Pif



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!