如何解决如何测试从本地调用到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 举报,一经查实,本站将立刻删除。