如何测试从本地调用到Flutter的方法通道?

问题描述

我正在开发本机插件并尝试进行单元测试。 所有单元测试都将在Dart中完成(无本机代码)。

Flutter有一个测试示例,说明如何使用setMockMethodCallHandler测试从Dart到本机的调用方法通道。

问题是我还没有找到一种方法来测试从本地调用到使用setMethodCallHandler处理来自本地的调用的Dart的方法通道。

这是一个例子

// main.dart

class Plugin {

  static MethodChannel _channel = const MethodChannel('plugin');
 
  Plugin() {
    _channel.setMethodCallHandler((call) async {
      print("called from native: ${call.method}");
    });
  }

}
// tests/main_test.dart

void main() {
  const MethodChannel _channel = MethodChannel('core.super_router');
  Plugin plugin;

  setUp(() async {
    TestWidgetsFlutterBinding.ensureInitialized();
    plugin = Plugin();
  });

  test("call from native",() async {
    _channel.invokeMethod("something");
    // This call can't reach the handler in the Plugin
    // And there is no method like mockInvokeMethod
  });
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...