简体   繁体   English

单元测试失败,断言失败:第 17 行:'<optimized out> ':不正确</optimized>

[英]unit test fails with Failed assertion: line 17: '<optimized out>': is not true

I'm stuck with a unit test and fairly new to testing.我坚持进行单元测试并且对测试相当陌生。 I tried to create a test for a class with toMap/fromMap methods and write tests for it.我尝试使用 toMap/fromMap 方法为 class 创建测试并为其编写测试。

Within the class I have the following code, where I added the hashCode and == operator methods to prepare the class for the test.在 class 中,我有以下代码,其中我添加了 hashCode 和 == 运算符方法来准备 class 进行测试。

I have the exact same setup for other classes, where the test works..对于测试有效的其他类,我有完全相同的设置..

  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;
  }
}

And this is the test that fails:这是失败的测试:

      final userCase = UserCase.fromData({
        'uid': 'id123',
        'email': 'email123',
        'userName': 'username',
        'diagnose': 'ACL',
        'surgery': true,
        'goals': [],
        'kos': [],
      });
      expect(
          userCase,
          UserCase(
            uid: 'id123',
            email: 'email123',
            userName: 'username',
            diagnose: 'ACL',
            surgery: true,
            goals: [],
            kos: [],
          ));
    });

**UPDATED ERROR MESSAGE after hashList has been added ** **添加 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>```

And that is the error message - this one changed now to the one above, after adding the hashList method to the list properties.这就是错误消息 - 在将 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>´´´

I can't see anything obvious suggesting which property is causing the test to fail.我看不到任何明显的迹象表明哪个属性导致测试失败。

A couple of suggestions:几个建议:

  • Try removing one property at a time from the comparisons in the == implementation.尝试从 == 实现中的比较中一次删除一个属性。
  • Try using the Equatable package for your model class.尝试为您的 model class 使用Equatable package。 This reduces the boilerplate a lot and is less error-prone than implementing == and hashCode by hand.这大大减少了样板文件,并且比手动实现==hashCode更不容易出错。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单元测试断言在几种情况下都是正确的 - Unit test assertion that is true in several cases Grails断言 - 验证断言在单元测试中失败 - Grails assert - verify that assertion failed in unit test Rails单元测试之谜:方法运行,值更改,断言失败 - Rails Unit Test Mystery: Method Runs, Value is Changed, Assertion Fails Rails单元测试-找出断言为什么确切失败的原因 - Rails unit testing - Find out why exactly the assertion failed 单元测试默认的断言方法? - Unit test default Assertion method? 协调器上的单元测试断言失败,因为视图 controller 需要一些时间才能推送到导航堆栈 - Unit test assertion on a coordinator fails because the view controller takes some time to be pushed on the navigation stack MSTest单元测试在“调试”模式下通过,但在“运行”模式下未通过最终断言 - MSTest unit test passes when in “debug” mode, but fails final assertion in “run” mode 单元测试因GrailsConfigurationException失败-无法找到负责任的代码行 - Unit Test fails with GrailsConfigurationException - cannot find responsible line of code 单元测试失败-为什么? - Unit Test Fails — Why? MaxLengthAttribute 的单元测试失败 - Unit Test of MaxLengthAttribute fails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM