flutter - Flutter:当我使用fluttertoast时执行单元测试
问题描述
我正在尝试测试开发并习惯它,但是当我对我的函数执行单元测试时出现某种类型的错误,该函数仅验证消息的长度,但如果验证失败返回一个布尔值并显示敬酒。
因为我只作为单元测试执行,所以 toast 没有 buildContext 来显示消息。
我想知道是否有一种方法可以在我测试的代码中执行这种过程
class ContactUsLogic {
static final FirebaseFirestore _db = FirebaseFirestore.instance;
static late CollectionReference _collectionReference;
// static DocumentReference _documentReference;
static Future<bool> addComents(String message) async {
if (message.trim().length < 30) {
Utilities.showToast(
Strings.CONTACT_LABEL_MIN_LENGTH, Toast.LENGTH_LONG, 3);
return false;
}
if (message.trim().length > 400) {
Utilities.showToast(
Strings.CONTACT_LABEL_MAX_LENGTH, Toast.LENGTH_LONG, 3);
return false;
}
final User user = authService.getUser!;
_collectionReference =
_db.collection(Utilities.formatPath([Strings.FS_COL_COMMENTS]));
await _collectionReference.doc().set({
"uid": user.uid,
"email": user.email,
"comments": message,
"createdDate": FieldValue.serverTimestamp(),
});
Utilities.showToast(Strings.CONTACT_SUCCESS, Toast.LENGTH_LONG, 3);
return true;
}
}
这是测试文件
import 'package:flutter_test/flutter_test.dart';
import 'package:project_cat/controller/contact_us_logic.dart';
void main() {
test('send comment with message empty', () async {
assert(await ContactUsLogic.addComents(""), false);
});
}
函数 showToast 只能这样调用fluttertoast包的函数showToast
static void showToast(String message, Toast lenght, int timeIos) {
Fluttertoast.showToast(
msg: message,
toastLength: lenght,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: timeIos);
}
我执行时的错误是
00:02 +0 -1:
Null check operator used on a null value
package:flutter/src/services/platform_channel.dart 142:86 MethodChannel.binaryMessenger
package:flutter/src/services/platform_channel.dart 148:36 MethodChannel._invokeMethod
package:flutter/src/services/platform_channel.dart 331:12 MethodChannel.invokeMethod
package:fluttertoast/fluttertoast.dart 98:32 Fluttertoast.showToast
package:project_cat/customClasses/utilities.dart 19:18 Utilities.showToast
package:project_cat/controller/contact_us_logic.dart 15:17 ContactUsLogic.addComents
test\contact_us_test.dart 6:33 main.<fn>
test\contact_us_test.dart 5:43 main.<fn>
我感谢任何建议提前谢谢
解决方案
推荐阅读
- excel - 比较 VLOOKUP 中的 2 列后替换值
- postman - 调用 managedDevices 在 POSTMAN 中返回错误,但在 PowerShell 中有效
- javascript - 通过输入更改(keyup)更改 jquery 滑块位置
- python - 这是 Python 中某种类型的装饰器吗?
- java - 如何使用火花加入两个具有 OR 条件的数据集
- xamarin.forms - 在 Xamarin Forms 中控制单页方向和视图旋转
- javascript - 等待两个嵌套的 Promise.all 在 PromiseJS 中完成
- matlab - 对于 MATLAB gui 中的每个按钮,回调到某个文件中的某一行(指南)
- sql-server - 以零开头的 SQL Server ID
- android - 将用户数据(数据库)备份到在线存储的最佳方法