flutter - 如何在颤振中测试保存的文件
问题描述
这个flutter网页读写文件
提供了保存文件的示例,在本例中包含一个计数器。我已将其粘贴到示例应用程序中。如何对文件保存部分进行单元测试?我失败的测试尝试低于应用程序代码。有2个问题:
如何编写测试本身 - t = storage.writeCounter(counter); 的可测试返回是什么?
如何关闭应用程序并证明文件存在并且以后可以读取
main.dart
import 'package:flutter/foundation.dart';
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),
),
);
}
}
counter_test.dart
import 'package:write_file_test/main.dart';
void main() {
test('Check file save works', () async {
final CounterStorage storage;
var counter = 6;
var t = storage.writeCounter(counter);
expect(find.text('1'), findsOneWidget);
});
}
发布规范.yaml
name: write_file_test
description: Flutter test file saving
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1
environment:
sdk: ">=2.12.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
path_provider: ^2.0.5
cupertino_icons: ^1.0.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^1.0.0
flutter:
uses-material-design: true
解决方案
推荐阅读
- slim - 替换 Slim 中间件中请求对象的内容
- apache-spark - 在 spark return py4j.protocol.Py4JJavaError 中从 PostgreSQL 读取数据
- flutter - 如何将 Equatable 扩展到已经扩展和混合的类?
- python - 删除python中的重复项
- c - 更新 C 数组中的特定元素
- c# - linux上的c#.net核心证书链失败,windows工作
- gitlab - Github Desktop 无法从 Gitlab 克隆 repo
- php - 是什么导致 Composer 不更新到已修改的现有版本?
- python - 在Windows10中使用python弹出后如何监控系统托盘图标通知?
- android - 如何在 Flutter 上选择唯一的前置摄像头