如何正确模拟 Flutter Fire Firebase 函数?

Mig*_*alv 6 testing mocking flutter google-cloud-functions flutter-test

我正在尝试模拟 Flutter Fire 中的 Firebase Functions 包,但我不断收到错误。

这是我创建模拟的尝试。但是call当我尝试覆盖它时,该函数给了我错误,因为返回类型不正确。

library firebase_cloud_functions_mock;

import 'dart:convert';

import 'package:cloud_functions/cloud_functions.dart';
import 'package:mockito/mockito.dart';

class MockFirebaseFunctions extends Mock implements FirebaseFunctions {
  final Map<String, String> _jsonStore = <String, String>{};

  String _convertMapToJson(Map<String, dynamic> parameters) {
    return json.encode(parameters);
  }

  void mockResult(
      {String functionName, String json, Map<String, dynamic> parameters}) {
    if (parameters?.isNotEmpty != null) {
      // ignore: parameter_assignments
      functionName = functionName + _convertMapToJson(parameters);
    }
    _jsonStore[functionName] = json;
  }

  String getMockResult(String functionName, Map<String, dynamic> parameters) {
    // ignore: parameter_assignments
    functionName = parameters == null
        ? functionName
        : (parameters?.isNotEmpty != null
            ? functionName + _convertMapToJson(parameters)
            : functionName);
    assert(
        _jsonStore[functionName] != null, 'No mock result for $functionName');
    return _jsonStore[functionName];
  }

  @override
  HttpsCallable getHttpsCallable({String functionName}) {
    return HttpsCallableMock._(this, functionName);
  }
}

class HttpsCallableMock extends Mock implements HttpsCallable {
  HttpsCallableMock._(this._firebaseFunctions, this._functionName);

  final MockFirebaseFunctions _firebaseFunctions;
  final String _functionName;

  @override
  Future<HttpsCallableResult> call([dynamic parameters]) {
    final decoded = json.decode(_firebaseFunctions.getMockResult(
        _functionName, parameters as Map<String, dynamic>));
    return Future.value(HttpsCallableResultMock._(decoded));
  }

  /// The timeout to use when calling the function. Defaults to 60 seconds.
  Duration timeout;
}

class HttpsCallableResultMock extends Mock implements HttpsCallableResult {
  HttpsCallableResultMock._(this.data);

  /// Returns the data that was returned from the Callable HTTPS trigger.
  @override
  final dynamic data;
}
Run Code Online (Sandbox Code Playgroud)

有谁知道如何正确模拟 Flutter Fire 中的 Firebase Functions 包?

Ray*_*ers 3

我能够使您的示例与cloud_function-2.0.0一起使用,只需进行三个小更改。

  1. <T>HttpsCallableMock.call和 中添加类型参数HttpsCallableResultMock

  2. 将 的覆盖替换 getHttpsCallablehttpsCallable.

  3. parameters?.isNotEmpty == true改为检查!= null。可能不需要进行此更改,具体取决于传递空参数时您想要的行为。

这是更新后的代码。

import 'package:cloud_functions/cloud_functions.dart';
import 'package:mockito/mockito.dart';

class MockFirebaseFunctions extends Mock implements FirebaseFunctions {
  final Map<String, String> _jsonStore = <String, String>{};

  String _convertMapToJson(Map<String, dynamic> parameters) {
    return json.encode(parameters);
  }

  void mockResult(
      {String functionName, String json, Map<String, dynamic> parameters}) {
    if (parameters?.isNotEmpty == true) {
      functionName = functionName + _convertMapToJson(parameters);
    }
    _jsonStore[functionName] = json;
  }

  String getMockResult(String functionName, Map<String, dynamic> parameters) {
    // ignore: parameter_assignments
    functionName = parameters == null
        ? functionName
        : (parameters?.isNotEmpty == true
        ? functionName + _convertMapToJson(parameters)
        : functionName);
    assert(
    _jsonStore[functionName] != null, 'No mock result for $functionName. \n Expected one of ${_jsonStore.keys}');
    return _jsonStore[functionName];
  }

  @override
  HttpsCallable httpsCallable(String functionName, {HttpsCallableOptions options}) {
    return HttpsCallableMock._(this, functionName);
  }
}

class HttpsCallableMock extends Mock implements HttpsCallable {
  HttpsCallableMock._(this._firebaseFunctions, this._functionName);

  final MockFirebaseFunctions _firebaseFunctions;
  final String _functionName;

  @override
  Future<HttpsCallableResult<T>> call<T>([dynamic parameters]) {
    final decoded = json.decode(_firebaseFunctions.getMockResult(
        _functionName, parameters as Map<String, dynamic>));
    return Future.value(HttpsCallableResultMock<T>._(decoded));
  }

  /// The timeout to use when calling the function. Defaults to 60 seconds.
  Duration timeout;
}

class HttpsCallableResultMock<T> extends Mock implements HttpsCallableResult<T> {
  HttpsCallableResultMock._(this.data);

  /// Returns the data that was returned from the Callable HTTPS trigger.
  @override
  final T data;
}

Run Code Online (Sandbox Code Playgroud)