[英]Flutter unit tests on Windows
我有一個正在開發的顫振包,如果我為 Windows 構建它,它會構建,從輸出中我可以看到它工作正常。 但是,當我嘗試將測試作為測試運行(而不是構建它)時,我總是會收到 MissingPluginException(No implementation found for method) 錯誤。 就好像測試沒有使用實際構建應用程序正在使用的東西。
有解決辦法嗎? 我在運行測試之前調用了 TestWidgetsFlutterBinding.ensureInitialized() ,但總是得到同樣的錯誤......
測試代碼(構建時有效):
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
final networkHealth = IwNetworkHealth();
test('returns ip address', () async {
print("WifiIP: ${await networkHealth.getWifiIp()}");
});
}
實際調用代碼:
Future<String?> getWifiIp() async{
var info = NetworkInfo();
var wifiIP = await info.getWifiIP();
return wifiIP;
}
感謝@lepsch 在評論中的指導,似乎確實需要模擬任何多平台插件。 我現在可以通過添加自己的處理程序來攔截通道方法來運行測試,這使我可以正確測試我的代碼。 這是我現在正在使用的測試設置功能的示例:
setUpAll(() {
const networkInfoChannel = MethodChannel('dev.fluttercommunity.plus/network_info');
networkInfoHandler(MethodCall methodCall) async {
if (methodCall.method == 'wifiIPAddress') {
return Future<String?>.delayed(const Duration(milliseconds: 100)).then((value) => "192.168.0.20");
}
return "unknown";
}
TestWidgetsFlutterBinding.ensureInitialized();
TestDefaultBinaryMessengerBinding.instance?.defaultBinaryMessenger
.setMockMethodCallHandler(networkInfoChannel, networkInfoHandler);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.