-
Notifications
You must be signed in to change notification settings - Fork 42
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
Mocking dio : Could not find mocked route matching #96
Comments
Hi @tazik561 ! The problem is you're throwing a expect(() async => await dio.post(path), throwsA(isA<AdapterError>())); But you was given a dioAdapterMockito.onPost(
path,
(request) => request.throws(500, dioError),
headers: {'Content-Type': 'application/json; charset=utf-8'},
); So I mean you should change your expected value as expect(() async => await dio.post(path), throwsA(isA< DioError>())); The full test code: void main() {
group("Splash init", () {
DioAdapterMockito dioAdapterMockito;
Dio dio;
const path = 'https://..../mobile/';
setUpAll(() {
dioAdapterMockito = DioAdapterMockito();
dio = Dio()..httpClientAdapter = dioAdapterMockito;
});
test(
"call getMainConfigs -> getMainConfig web service called 404 exception",
() async {
final dioError = DioError(
error: {'message': 'Some beautiful error!'},
requestOptions: RequestOptions(path: path),
response: Response(
statusCode: 500,
requestOptions: RequestOptions(path: path),
),
type: DioErrorType.response,
);
dioAdapterMockito.onPost(
path,
(request) => request.throws(500, dioError),
headers: {'Content-Type': 'application/json; charset=utf-8'},
);
expect(() async => await dio.post(path), throwsA(isA<AdapterError>()));
// expect(() async => await dio.get(path), throwsA(isA<DioError>()));
// expect(
// () async => await dio.get(path),
// throwsA(
// predicate(
// (DioError error) =>
// error is DioError &&
// error is AdapterError &&
// error.message == dioError.error.toString(),
// ),
// ),
// );
});
});
} Correct and clean usage of http-mock-adapter should be:void main() {
DioAdapter dioAdapterMockito;
Dio dio;
DioError dioError;
const path = 'https://example.com';
setUpAll(() {
dioAdapterMockito = DioAdapterMockito();
dio = Dio();
dio.httpClientAdapter = dioAdapterMockito;
DioError(
error: {'message': 'Some beautiful error!'},
requestOptions: RequestOptions(path: path),
response: Response(
statusCode: 500,
requestOptions: RequestOptions(path: path),
),
type: DioErrorType.response,
);
});
group("Splash init", () {
test(
"call getMainConfigs -> getMainConfig web service called 404 exception",
() async {
dioAdapterMockito.onPost(
path,
(request) => request.throws(500, dioError),
headers: {'Content-Type': 'application/json; charset=utf-8'},
);
expect(() async => await dio.post(path), throwsA(isA<DioError>()));
});
});
}
|
Thank . I changed my code to this:
But I got this error:
|
@tazik561 are you using the latest version of http_mock_adapter? |
Don't create a variable for expect(() async => await dio.post(path, data: null),
throwsA(isA<DioError>())); Not this: var result = await dio.post(path, data: null);
expect(() async => result, throwsA(isA<DioError>())); And make sure you're using latest versions of Here is the full code of your test:group("Dio Exception", () {
DioAdapterMockito dioAdapterMockito;
Dio dio;
DioError dioError;
const path = 'https://..../mobile/';
setUpAll(() {
dioAdapterMockito = DioAdapterMockito();
dio = Dio()..httpClientAdapter = dioAdapterMockito;
dioError = DioError(
error: {'message': 'Some beautiful error!'},
requestOptions: RequestOptions(path: path),
response: Response(
statusCode: 500,
requestOptions: RequestOptions(path: path),
),
type: DioErrorType.response,
);
});
test(
"call getMainConfigs -> getMainConfig web service called 404 exception",
() async {
dioAdapterMockito.onPost(
path,
(request) => request.throws(500, dioError),
headers: {'Content-Type': 'application/json; charset=utf-8'},
);
expect(() async => await dio.post(path, data: null),
throwsA(isA<DioError>()));
});
}); |
Of course not. I am using It is possible to mix DioAdapterMockito in this way ):
main method on repo class:
to check a method that has dio method inside it? |
@LukaGiorgadze no Luka, we were resolved this issue with #100 PR. @tazik561 's issue needn't any changes on http-mock-adapter, he just should do this , so the problem is his test code. |
I don't want to test dio directly . As I mention above I have a method called :
I am trying write test for this method In this answer we just test dio directly. But I want to test a method that has dio part like repository layer. for example
I call |
same problem here |
Well, I'm trying to use this with getIt.registerLazySingleton(
() => BaseOptions(baseUrl: 'https://www.bscotch.net/api/levelhead'),
instanceName: 'rumpusClientBaseOptions');
getIt.registerLazySingleton(
() => Dio(getIt.get(instanceName: 'rumpusClientBaseOptions')),
instanceName: 'rumpusClient'); Then in setUpAll(() async {
await setUpDI();
var adapter = DioAdapter();
adapter
..onGet(
'/players',
(request) => request.reply(200, {
'data': [
{
"_id": "609e5516f0b9d200b711b8b5",
"userId": "pvdw78",
"stats": {
"Subscribers": 0,
"NumFollowing": 0,
"Crowns": 0,
"Shoes": 0,
"PlayTime": 0,
"TipsPerLevel": 0,
"TipsPerDay": 0,
"TippedPerLevelPlayed": 0,
"TippedPerDay": 0,
"HiddenGem": 0,
"Trophies": 0,
"PerkPoints": 5,
"CampaignProg": 0,
"TimeTrophies": 0
},
"createdAt": "2021-05-14T10:46:46.723Z",
"updatedAt": "2021-05-14T10:46:48.639Z",
"alias": {
"userId": "pvdw78",
"alias": "LeapyimbleZiprompa",
"avatarId": "gr18-serious",
"context": "levelhead"
}
}
]
}));
Dio client = getIt.get(instanceName: 'rumpusClient');
client.httpClientAdapter = adapter;
}); Since it is a lazy singleton, I'm sure I get the only one instance of
Opting out of Environment
|
Hi @erayerdin first of all thanks for your response! In your test code I can see you did mocking some request by this path:
and you get error by this path:
So your test code's path and real code's path are different. |
@tazik561 Okay, as I understand it, you have a |
I have had several cases where the mocked route could not be found. It was always my mistake but it is hard to trace down with the current assertion errors. I suggest we introduce a custom error messages with diffs for data/headers/params, especially if there is only one mocked response. With more than one mocked response it might get a bit harder to generate a correct diff. |
Hi guys! I'm trying to solve this and there is no way!
And trying to test errors and exceptions with this:
But could not resolve correctly the expectation:
Any thoughts? Thanks in advance! |
I am trying to make unit test with mockitto to test dio.
but after running this test I got this error:
I am trying to test exception.
The text was updated successfully, but these errors were encountered: