Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.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
Unit testing 单元测试失败,断言失败:第17行:'&书信电报;优化输出>';:事实并非如此_Unit Testing_Flutter_Dart_Testing - Fatal编程技术网

Unit testing 单元测试失败,断言失败:第17行:'&书信电报;优化输出>';:事实并非如此

Unit testing 单元测试失败,断言失败:第17行:'&书信电报;优化输出>';:事实并非如此,unit-testing,flutter,dart,testing,Unit Testing,Flutter,Dart,Testing,我被一个单元测试困住了,而且对测试还相当陌生。 我试图用toMap/fromMap方法为一个类创建一个测试,并为它编写测试 在这个类中,我有以下代码,其中我添加了hashCode和==操作符方法来为测试准备类 我对其他类有完全相同的设置,在这些类中测试是有效的 String uid; String email; String userName; String diagnose; bool surgery; List<Goal> goals; List<

我被一个单元测试困住了,而且对测试还相当陌生。 我试图用toMap/fromMap方法为一个类创建一个测试,并为它编写测试

在这个类中,我有以下代码,其中我添加了hashCode和==操作符方法来为测试准备类

我对其他类有完全相同的设置,在这些类中测试是有效的

  String uid;
  String email;
  String userName;
  String diagnose;
  bool surgery;
  List<Goal> goals;
  List<KOS> kos;

  UserCase({
    this.uid,
    this.email,
    this.userName,
    this.diagnose,
    this.surgery = false,
    this.goals,
    this.kos,
  });

  factory UserCase.fromData(Map<String, dynamic> data) {
    if (data == null) {
      print('UserCase fromData NULL');
      return null;
    }
    final String uid = data['uid'] ?? '';
    final String email = data['email'] ?? '';
    final String userName = data['userName'] ?? '';
    final String diagnose = data['diagnose'] ?? '';
    final bool surgery = data['surgery'] ?? false;
    final List<Goal> goals = data['goals'] == null
        ? []
        : List.from(data['goals'].map((e) => Goal.fromData(e)));
    final List<KOS> kos = data['kos'] == null
        ? []
        : List.from(data['kos'].map((e) => KOS.fromData(e)));

    return UserCase(
      uid: uid,
      email: email,
      userName: userName,
      diagnose: diagnose,
      surgery: surgery,
      goals: goals,
      kos: kos,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'uid': uid,
      'email': email,
      'userName': userName,
      'diagnose': diagnose,
      'surgery': surgery,
      'goals': goals == null || goals.isEmpty
          ? []
          : goals.map((e) => e.toMap()).toList(),
      'kos':
          kos == null || kos.isEmpty ? [] : kos.map((e) => e.toMap()).toList(),
    };
  }

**UPDATE: adding hashList to ListObjects**

@override
int get hashCode => hashValues(
      uid, email, userName, diagnose, surgery, hashList(goals), hashList(kos));

old:
  *@override
  int get hashCode {
    return hashValues(uid, email, userName, diagnose, surgery, goals, kos);
  }*

  @override
  bool operator ==(other) {
    if (identical(this, other)) return true;
    if (runtimeType != other.runtimeType) return false;
    final UserCase otherCase = other;
    return uid == otherCase.uid &&
        email == otherCase.email &&
        userName == otherCase.userName &&
        diagnose == otherCase.diagnose &&
        surgery == otherCase.surgery &&
        goals == otherCase.goals &&
        kos == otherCase.kos;
  }
}
**添加hashList后更新的错误消息**

test/usercase_test.dart: fromData case with all properties [E]                                                               
  Expected: UserCase:<uid: id123, email: email123, userName: username, diagnose: ACL, surgery: true, goals: [], kos: []>
    Actual: UserCase:<uid: id123, email: email123, userName: username, diagnose: ACL, surgery: true, goals: [], kos: []>
  
  package:test_api                                   expect
  package:flutter_test/src/widget_tester.dart 431:3  expect
  usercase_test.dart 21:7                            main.<fn>.<fn>```
test/usercase\u test.dart:fromdatacase和所有属性[E]
预期:UserCase:
实际:用户案例:
包:test_api expect
包:颤振测试/src/widget测试仪。dart 431:3预期
usercase_test.dart 21:7 main```
这就是错误消息——在将hashList方法添加到列表属性之后,这个消息现在更改为上面的消息

.../test/usercase_test.dart: fromData case with all properties [E]                          
  'dart:ui/hash_codes.dart': Failed assertion: line 17: '<optimized out>': is not true.
  dart:ui                                            hashValues
  ../lib/Classes/UserCase.dart 71:12                 UserCase.hashCode
  dart:collection                                    _CompactLinkedHashSet.contains
  package:matcher/src/pretty_print.dart 28:14        prettyPrint._prettyPrint
  package:matcher/src/pretty_print.dart 119:22       prettyPrint
  package:matcher/src/description.dart 49:11         StringDescription.addDescriptionOf
  package:matcher/src/equals_matcher.dart 267:19     _DeepMatcher.describe
  package:matcher/src/description.dart 47:13         StringDescription.addDescriptionOf
  package:test_api                                   expect
  package:flutter_test/src/widget_tester.dart 431:3  expect
  usercase_test.dart 22:7                            main.<fn>.<fn>´´´
../test/usercase\u test.dart:fromData-case和所有属性[E]
“dart:ui/hash_code.dart”:失败的断言:第17行:“”:不正确。
dart:ui哈希值
../lib/Classes/UserCase.dart 71:12 UserCase.hashCode
dart:collection\u CompactLinkedHashSet.contains
包装:matcher/src/pretty_print.dart 28:14预打印。_预打印
包装:matcher/src/pretty_print.dart 119:22预打印
包:matcher/src/description.dart 49:11 StringDescription.addDescriptionOf
包装:matcher/src/equals_matcher.dart 267:19_DeepMatcher.description
包:matcher/src/description.dart 47:13 StringDescription.addDescriptionOf
包:test_api expect
包:颤振测试/src/widget测试仪。dart 431:3预期
usercase_test.dart 22:7 main

我看不到任何明显的迹象表明是哪个属性导致测试失败

有几点建议:

  • 尝试一次从==实现中的比较中删除一个属性
  • 尝试为您的模型类使用。与手动实现
    ==
    hashCode
    相比,这大大减少了样板文件,并且更不容易出错

我实际上认为,当将hashValues方法更改为:``@override int get hashCode=>hashValues(uid、电子邮件、用户名、诊断、手术、hashList(goals)、hashList(kos))时,我更接近了一步```非常感谢!Equalable软件包确实解决了这个问题!
.../test/usercase_test.dart: fromData case with all properties [E]                          
  'dart:ui/hash_codes.dart': Failed assertion: line 17: '<optimized out>': is not true.
  dart:ui                                            hashValues
  ../lib/Classes/UserCase.dart 71:12                 UserCase.hashCode
  dart:collection                                    _CompactLinkedHashSet.contains
  package:matcher/src/pretty_print.dart 28:14        prettyPrint._prettyPrint
  package:matcher/src/pretty_print.dart 119:22       prettyPrint
  package:matcher/src/description.dart 49:11         StringDescription.addDescriptionOf
  package:matcher/src/equals_matcher.dart 267:19     _DeepMatcher.describe
  package:matcher/src/description.dart 47:13         StringDescription.addDescriptionOf
  package:test_api                                   expect
  package:flutter_test/src/widget_tester.dart 431:3  expect
  usercase_test.dart 22:7                            main.<fn>.<fn>´´´