我在Flutter网站上阅读了introduction to platform-specific plugins/channels,并浏览了一些简单的插件示例,如url_launcher
:
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:flutter/services.dart';
const _channel = const MethodChannel('plugins.flutter.io/url_launcher');
/// Parses the specified URL string and delegates handling of it to the
/// underlying platform.
///
/// The returned future completes with a [PlatformException] on invalid URLs and
/// schemes which cannot be handled, that is when [canLaunch] would complete
/// with false.
Future<Null> launch(String urlString) {
return _channel.invokeMethod(
'launch',
urlString,
);
}
在小部件测试或集成测试中,我如何模拟或存根通道,这样我就不必依赖于真实的的设备(运行Android或iOS),比如实际启动URL?
4条答案
按热度按时间twh00eeo1#
MethodChannel#setMockMethodCallHandler
已弃用并已删除。看起来这是现在要走的路:
详细信息在GitHub上。
下面是
package_info
插件的测试示例,供将来参考:t98cgbkg2#
您可以使用setMockMethodCallHandler为底层方法通道注册一个模拟处理程序:
https://docs.flutter.io/flutter/services/MethodChannel/setMockMethodCallHandler.html
eaf3rand3#
当你创建一个插件时,系统会自动为你提供一个默认测试:
让我添加一些关于它的注解:
setMockMethodCallHandler
可以让你绕过实际插件所做的任何事情,并返回你自己的值。methodCall.method
来区分方法,methodCall.method
是被调用方法名称的字符串。vwoqyblh4#
在我的情况下,我的频道是私人的,所以我无法访问它。总的来说,我有这样的东西: