Giter Club home page Giter Club logo

Comments (2)

GiorgiBeriashvili avatar GiorgiBeriashvili commented on May 26, 2024 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:

  1. Create Dio, DioAdapter, and RestClient instances and assign dioAdapter to dio.httpClientAdapter;
  2. Leverage request handlers such as onPost to mock the behavior of request/reply processes;
  3. 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.

GiorgiBeriashvili avatar GiorgiBeriashvili commented on May 26, 2024

I will close this as it is seemingly resolved. Feel free to re-open.

from http-mock-adapter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.