Comments (2)
Hello!
I tested and mocked multiple possible requests by using Retrofit just to be sure that everything is working, and I will share below what I think is the solution for your original example case:
import 'package:http_mock_adapter/http_mock_adapter.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:retrofit/retrofit.dart';
import 'package:dio/dio.dart';
import 'package:test/test.dart';
part 'example_test.g.dart';
@RestApi(baseUrl: 'https://example.com')
abstract class RestClient {
factory RestClient(Dio dio, {String baseUrl}) = _RestClient;
@POST('/loadConfig')
Future<MainModel> getMainConfig(@Body() request);
}
@JsonSerializable()
class MainModel {
String versionName;
String currentMenuVersion;
String test;
MainModel({
required this.versionName,
required this.currentMenuVersion,
required this.test,
});
factory MainModel.fromJson(Map<String, dynamic> json) => _$MainModelFromJson(
json,
);
Map<String, dynamic> toJson() => _$MainModelToJson(this);
}
void main() {
test('Mocks Retrofit client POST request as defined', () async {
final dio = Dio();
final dioAdapter = DioAdapter();
final restClient = RestClient(dio);
const configPayload = <String, dynamic>{
'versionName': '1.0',
'currentMenuVersion': '2.0',
'test': 'test',
};
final mainModelResponse = MainModel(
versionName: 'versionName Reply',
currentMenuVersion: 'currentMenuVersion Reply',
test: 'test Reply',
);
dio.httpClientAdapter = dioAdapter;
dioAdapter.onPost(
'/loadConfig',
(request) => request.reply(200, mainModelResponse),
data: configPayload,
);
final response = await restClient.getMainConfig(configPayload);
print(response.toJson());
expect(mainModelResponse.toJson(), response.toJson());
});
}
Of course there is also generated example_test.g.dart
which I won't include since it's not as relevant.
The result:
{versionName: versionName Reply, currentMenuVersion: currentMenuVersion Reply, test: test Reply}
✓ Mocks Retrofit client POST request as defined
The essential part is same as mocking Dio
, there are generally following steps you can take:
- Create
Dio
,DioAdapter
, andRestClient
instances and assigndioAdapter
todio.httpClientAdapter
; - Leverage request handlers such as
onPost
to mock the behavior of request/reply processes; - Send requests from
restClient
and check if you got the expected predefined responses.
I think it is fairly straightforward. A good candidate to be added in documentation/examples for sure.
Thank you for bringing this issue to our attention! I hope I answered your question. Please let me know if there's anything else you are interested in or if the issue is resolved.
from http-mock-adapter.
I will close this as it is seemingly resolved. Feel free to re-open.
from http-mock-adapter.
Related Issues (20)
- Data on onPost is compared before it's encoded to json HOT 1
- route matches ignore `baseUrl`
- Selective mocking HOT 11
- can i request to onDownload when add unit test ? HOT 2
- [v0.4.0] collection version conflict between the v0.4.0 and flutter_test HOT 9
- Lower the Dart SDK constraint to 2.15.0
- Let server return different responses HOT 7
- FullHttpRequestMatcher: not identical data returns success response HOT 3
- Using query parameters in response data generation HOT 2
- use tester.pump rather than Future.delayed to allow tests to run faster HOT 1
- Split up `MockServer` `reply` method into static and dynamic version HOT 11
- http_mock_adapter 0.5.0 conflicts with flutter_test collection version. HOT 8
- Async mocking not possible HOT 1
- Semantic release with github action HOT 2
- Add methods onDownload & onDownloadUri
- Library not compatible with flutter 3.16.0 - incorrect type definition HOT 3
- LDClient.get() was called before init()!
- Ability to mock whole API interface for local development without a need of a server. HOT 2
- Allow timeout simulations in replies HOT 2
- QUESTION: how to use dio.simpleGet HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from http-mock-adapter.