Skip to content
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

You need a MockInterceptor() for mock data #115

Closed
ozburo opened this issue Jan 13, 2020 · 7 comments
Closed

You need a MockInterceptor() for mock data #115

ozburo opened this issue Jan 13, 2020 · 7 comments
Labels
documentation Documentation related

Comments

@ozburo
Copy link

ozburo commented Jan 13, 2020

How can we implement a Mock Data Interceptor like the one we have for DIO?
https://github.com/gabuldev/dio_interceptors/blob/master/lib/src/interceptor_mock.dart

I can see a way to modify either the outgoing request or incoming response, but can't skip the request call (no need) and simply return a Map/Data.

Is this possible w/ Chopper? I think it would be a welcomed addition 👍

@lejard-h lejard-h added the documentation Documentation related label Feb 24, 2020
@lejard-h
Copy link
Owner

Chopper use package:http.

You should be able to directly use the mocking API of the HTTP package.
https://pub.dev/documentation/http/latest/testing/MockClient-class.html

@ozburo
Copy link
Author

ozburo commented Apr 3, 2020

For anyone wondering how to do this I just came back to this issue and refactored our app back to using Chopper and implemented mock data like this:

import 'dart:convert';

import 'package:chopper/chopper.dart';
import 'package:http/http.dart' as http;
import 'package:http/testing.dart';

part 'api_service.chopper.dart';

@ChopperApi()
abstract class ApiService extends ChopperService {
  static ApiService create() {
    final client = ChopperClient(
      client: MockClient((request) async {
        Map result = mockData[request.url.path]?.firstWhere((d) {
          if (d['id'] == request.url.queryParameters['id']) return true;
          return false;
        }, orElse: () => null);
        if (result == null) {
          return http.Response(
              json.encode({'error': "not found"}), 404);
        }
        return http.Response(json.encode(result), 200);
      }),
      baseUrl: 'https://mysite.com/api',
      services: [
        _$ApiService(),
      ],
      converter: JsonConverter(),
      errorConverter: JsonConverter(),
    );
    return _$ApiService(client);
  }

  @Get(path: "/get")
  Future<Response> get(@Query() String url);
}

This assumes mockData is a List of Dart objects keyed by our url path /get and our request url is something like http://some.com/get?id=123

It's easy and elegant once you know how 💯

@JEuler
Copy link
Collaborator

JEuler commented Apr 10, 2020

@ozburo Nice thing! Can we use this code for somekind of documentation about mocking? @lejard-h Are you agree? :)

@ozburo
Copy link
Author

ozburo commented Apr 10, 2020

@JEuler that's why I posted it 😊

@JEuler JEuler mentioned this issue Apr 10, 2020
@JEuler
Copy link
Collaborator

JEuler commented Apr 11, 2020

Thank you!

@JEuler
Copy link
Collaborator

JEuler commented Apr 16, 2020

Should we close this guys? :)

@JEuler
Copy link
Collaborator

JEuler commented Apr 17, 2020

Feel free to reopen :)

@JEuler JEuler closed this as completed Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Documentation related
Projects
None yet
Development

No branches or pull requests

3 participants