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>());
});
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With