【问题标题】:Why ApplicationsDocumentsDirectory return null for unit test?为什么 ApplicationsDocumentsDirectory 为单元测试返回 null?
【发布时间】:2019-10-03 03:34:15
【问题描述】:

我正在使用颤振“path_provider”插件。我需要一个 SQLite 操作。我的错误测试类没有找到“getApplicationDocumentsDirectory”并返回 null。 该应用程序在模拟器/真实设备上运行任何工作都没有问题。

正在寻找提供者存储库和测试文件夹。我已经厌倦了测试类示例,但错误仍然存​​在。

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

  channel.setMockMethodCallHandler((MethodCall methodCall) async {
    log.add(methodCall);
    return response;
  });

  test('user save data', () async {
    var response = null;
//FIXME : directory return null
    final Directory directory = await getApplicationDocumentsDirectory();
    final model = UserWordInformation();
    model.word = word;
    model.know = 1;
    final result = await dbHelper.insert(model.toMap());
    expect(result, 1);
  });

我希望设备的返回路径文件夹。某些路径:“/Users/vb/Library/Developer/CoreSimulator/Devices/C5B3C94C-C774-4D0E-A19C-97AAF11BD9E3/data/Containers/Data/Application/0508712B-A138 -483A-921E-B5EAE6DF149F/文档"

【问题讨论】:

    标签: flutter flutter-plugin


    【解决方案1】:

    也许你忘记初始化你的 response 变量。

    在我的一个单元测试中,getApplicationDocumentsDirectory 遇到了类似的问题。

    MissingPluginException(在通道 plugins.flutter.io/path_provider 上找不到方法 getApplicationDocumentsDirectory 的实现)

    在单元测试文件中添加如下代码:

    const MethodChannel channel = MethodChannel('plugins.flutter.io/path_provider');
    channel.setMockMethodCallHandler((MethodCall methodCall) async {
      return ".";
    });
    

    现在它终于起作用了。 希望这会有所帮助。

    【讨论】:

    • 我想拥抱你
    • 我还必须添加TestWidgetsFlutterBinding.ensureInitialized()
    【解决方案2】:

    这个问题一般是因为缺少依赖,你的pubspec.yaml有以下依赖?

    依赖:

      path_provider: ^1.2.0
    
      simple_permissions: ^0.1.9
    

    如果 simple_permissions: ^0.1.9 在构建时抛出错误,请尝试使用此依赖项:

    path_provider: ^1.2.0
    
    permission_handler: ^3.2.0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-04
      • 2021-12-27
      • 2020-06-14
      • 2020-11-02
      • 2017-12-26
      • 2015-01-02
      • 2022-12-26
      相关资源
      最近更新 更多