写一个在 dart 中读写文件的测试

Write a test for reading and writing files in dart

我目前正在学习 Flutter 和 Dart。现在我想读写文件到内存。我有读写代码。现在我要对此进行测试。这是我 运行 遇到问题的地方。我总是得到:

'package:flutter/src/services/platform_channel.dart': Failed assertion: line 134 pos 7: '_binaryMessenger != null || ServicesBinding.instance != null': Cannot use this MethodChannel before the binary messenger has been initialized. This happens when you invoke platform methods before the WidgetsFlutterBinding has been initialized. You can fix this by either calling WidgetsFlutterBinding.ensureInitialized() before this or by passing a custom BinaryMessenger instance to MethodChannel().
dart:core                                                   _AssertionError._throwNew
package:flutter/src/services/platform_channel.dart 134:7    MethodChannel.binaryMessenger
package:flutter/src/services/platform_channel.dart 167:36   MethodChannel._invokeMethod
package:flutter/src/services/platform_channel.dart 350:12   MethodChannel.invokeMethod
package:path_provider_macos/path_provider_macos.dart 48:10  PathProviderMacOS.getApplicationDocumentsPath
package:path_provider/path_provider.dart 115:40             getApplicationDocumentsDirectory
package:skeet25pro/main_counter.dart 18:29                  CounterStorage._localPath
package:skeet25pro/main_counter.dart 24:24                  CounterStorage._localFile
package:skeet25pro/main_counter.dart 43:24                  CounterStorage.writeCounter
test/file_io_test.dart 8:27                                 main.<fn>
test/file_io_test.dart 5:33                                 main.<fn>

main_counter.dart

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

void main() {
  runApp(
    MaterialApp(
      title: 'Reading and Writing Files',
      home: FlutterDemo(storage: CounterStorage()),
    ),
  );
}

class CounterStorage {
  Future<String> get _localPath async {
    final directory = await getApplicationDocumentsDirectory();

    return directory.path;
  }

  Future<File> get _localFile async {
    final path = await _localPath;
    return File('$path/counter.txt');
  }

  Future<int> readCounter() async {
    try {
      final file = await _localFile;

      // Read the file
      final contents = await file.readAsString();

      return int.parse(contents);
    } catch (e) {
      // If encountering an error, return 0
      return 0;
    }
  }

  Future<File> writeCounter(int counter) async {
    final file = await _localFile;

    // Write the file
    return file.writeAsString('$counter');
  }
}

class FlutterDemo extends StatefulWidget {
  const FlutterDemo({Key? key, required this.storage}) : super(key: key);

  final CounterStorage storage;

  @override
  _FlutterDemoState createState() => _FlutterDemoState();
}

class _FlutterDemoState extends State<FlutterDemo> {
  int _counter = 0;

  @override
  void initState() {
    super.initState();
    widget.storage.readCounter().then((int value) {
      setState(() {
        _counter = value;
      });
    });
  }

  Future<File> _incrementCounter() {
    setState(() {
      _counter++;
    });

    // Write the variable as a string to the file.
    return widget.storage.writeCounter(_counter);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Reading and Writing Files'),
      ),
      body: Center(
        child: Text(
          'Button tapped $_counter time${_counter == 1 ? '' : 's'}.',
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

file_io_test.dart

import 'package:flutter_test/flutter_test.dart';
import 'package:skeet25pro/main_counter.dart';

void main() {
  test('Check file save works', () async {
    final CounterStorage storage = CounterStorage();
    var counter = 6;
    var t = await storage.writeCounter(counter);
    expect(1, 1);
  });
}

当我通过模拟器 运行 应用程序时,它运行得非常好。我真的很想得到测试 运行ning.

编辑:如果我尝试添加 WidgetsFlutterBinding.ensureInitialized();

void main() {
  test('Check file save works', () async {
    WidgetsFlutterBinding.ensureInitialized();
    final CounterStorage storage = CounterStorage();
    var counter = 6;
    var t = await storage.writeCounter(counter);
    expect(1, 1);
  });
}

我收到错误:

MissingPluginException(No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider_macos)
package:flutter/src/services/platform_channel.dart 175:7  MethodChannel._invokeMethod

似乎应该使用类似的东西:setMockMethodCallHandler 来拦截对不同目录提供程序的调用。仍然没有有效的解决方案。

你必须模拟 path_provider 调用并且可能将 WidgetsFlutterBinding.ensureInitialized(); 放在 main 的开头。我猜你想要像

这样的东西
Future<void> main() async {
  TestWidgetsFlutterBinding.ensureInitialized();

  setUpAll(() {
    const channel = MethodChannel(
      'plugins.flutter.io/path_provider_macos',
    );
    channel.setMockMethodCallHandler((MethodCall methodCall) async {
      switch (methodCall.method) {
        case 'getApplicationDocumentsDirectory':
          return "PATH_TO_MOCK_DIR";
        default:
      }
    });
  });

  test('Check file save works', () async {
    final CounterStorage storage = CounterStorage();
    var counter = 6;
    var t = await storage.writeCounter(counter);
    expect(1, 1);
  });
}```