Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振:使用颤振时执行单元测试_Flutter_Flutter Test - Fatal编程技术网

Flutter 颤振:使用颤振时执行单元测试

Flutter 颤振:使用颤振时执行单元测试,flutter,flutter-test,Flutter,Flutter Test,我试图测试开发并适应它,但是当我对我的函数执行单元测试时,我会遇到一些类型的错误,函数只验证消息的长度,但是如果验证失败,则返回bool并显示toast 因为我只作为单元测试执行,所以toast没有显示消息的buildContext 我想知道是否有一种方法可以执行我测试的代码中的这种过程 class ContactUsLogic { static final FirebaseFirestore _db = FirebaseFirestore.instance; static late C

我试图测试开发并适应它,但是当我对我的函数执行单元测试时,我会遇到一些类型的错误,函数只验证消息的长度,但是如果验证失败,则返回bool并显示toast

因为我只作为单元测试执行,所以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;
  }
}
函数showToast仅以这种方式调用包的函数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>
00:02+0-1:
对空值使用的空检查运算符
包:颤振/src/services/platform_channel.dart 142:86 MethodChannel.binaryMessenger
包:颤振/src/services/platform\u通道。dart 148:36方法通道。\u调用方法
包:颤振/src/services/platform_channel.dart 331:12 MethodChannel.invokeMethod
包装:fluttoast/fluttoast.dart 98:32 fluttoast.showtoots
包:project_cat/customClasses/utilities.dart 19:18 utilities.showtoos
包:项目\目录/控制器/联系人\逻辑。dart 15:17联系人逻辑。添加内容
测试\联系我们测试。dart 6:33 main。
测试\联系我们测试。dart 5:43 main。
我很感激你的建议,谢谢

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>