b.b*_*ben 3 flutter flutter-test
我正在开发一个本机插件并尝试进行单元测试。所有单元测试都将在 Dart 中完成(无本机代码)。
Flutter 有一个测试示例,说明如何使用 测试从 Dart 到本机的调用方法通道setMockMethodCallHandler。
问题是我还没有找到测试从本机调用到用于setMethodCallHandler处理来自本机调用的 Dart 的方法通道的方法。
这是一个例子
// main.dart
class Plugin {
static MethodChannel _channel = const MethodChannel('plugin');
Plugin() {
_channel.setMethodCallHandler((call) async {
print("called from native: ${call.method}");
});
}
}
Run Code Online (Sandbox Code Playgroud)
// 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
});
}
Run Code Online (Sandbox Code Playgroud)
下面的片段基于 CyrilHu 的方法,非常适合我。
test("Listening device", () async {
ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage(
'channnelName',
StandardMethodCodec().encodeMethodCall(
MethodCall('methodName', '{"result":true}')),(ByteData data){});
...
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3470 次 |
| 最近记录: |