2017-05-10 3 views
1

私はフラッタウェブサイト上introduction to platform-specific plugins/channelsを読んで、私はurl_launcherのように、プラグインのいくつかの簡単な例をブラウズ:私はしませんので、Flutterプラットフォームのチャンネル/プラグインを模擬/スタブするにはどうしたらいいですか?

ウィジェットのテストや統合テストで
// 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を起動していますか?

答えて

3

あなたが基本となるメソッドチャネルのモックハンドラを登録するsetMockMethodCallHandlerを使用することができます。

https://docs.flutter.io/flutter/services/MethodChannel/setMockMethodCallHandler.html

final List<MethodCall> log = <MethodCall>[]; 

MethodChannel channel = const MethodChannel('plugins.flutter.io/url_launcher'); 

// Register the mock handler. 
channel.setMockMethodCallHandler((MethodCall methodCall) async { 
    log.add(methodCall); 
}); 

await launch("http://example.com/"); 

expect(log, equals(<MethodCall>[new MethodCall('launch', "http://example.com/")])); 

// Unregister the mock handler. 
channel.setMockMethodCallHandler(null); 
+0

読者が理解してちょうどそう、MethodChannel'は、標準的な( 'const')オブジェクトである'ので、私はプラグインの作者が 'channel'をpublicにしていなくても自分のパッケージで' setMockMethodCallHandler'を作成できますか? – matanlurey

+0

プラグインは、デフォルトのものを使用する代わりにMethodChannel引数をとる@visibleForTestingコンストラクタを提供する必要があります。 url_launcherはまだこれを行いませんが、firebase_analyticsプラグインはこのパターンの例です。最終的には、ほとんどのプラグインがこのパターンに従い、よりテスト可能になることを期待しています。 https://github.com/flutter/firebase_analytics/blob/master/lib/firebase_analytics.dart –

関連する問題