gpt4 book ai didi

dart - Dart/颤振中的单元测试异常

转载 作者:行者123 更新时间:2023-12-03 04:39:47 25 4
gpt4 key购买 nike

我正在尝试使用给定的代码进行单元测试。

test('Given Employee When employeeName less than 5 then throws Exception', () async {
final employee = EmployeesCompanion.insert(
employeeName: 'Tony',
employeeCode: 'HR-121',
address: '5th Floor, Park Avenue',
contact: '1234567890',
hiringDate: DateTime.now());
expectLater(await employeesDB.createEmployee(employee),
throwsA((f) => f.toString().contains('employeeName: Must at least be 5 characters long')));
});
我的单元测试包括给定的谓词 throwsA((f) => f.toString().contains('employeeName: Must at least be 5 characters long')),但dart失败,但以下测试失败:
package:moor/src/runtime/data_verification.dart 74:5                  VerificationContext.throwIfInvalid
package:moor/src/runtime/query_builder/statements/insert.dart 197:51 InsertStatement._validateIntegrity
package:moor/src/runtime/query_builder/statements/insert.dart 96:5 InsertStatement.createContext
package:moor/src/runtime/query_builder/statements/insert.dart 64:17 InsertStatement.insert
package:moor_ex/src/db/employees.dart 79:76 EmployeesDB.createEmployee
test\employee_dept_test.dart 28:35 main.<fn>

InvalidDataException: Sorry, EmployeesCompanion(id: Value.absent(), employeeName: Value(Tony), employeeCode: Value(HR-121), address: Value(5th Floor, Park Avenue), contact: Value(1234567890), hiringDate: Value(2020-08-18 13:26:34.435349)) cannot be used for that because:
• employeeName: Must at least be 5 characters long.
那么用Dart编写此测试的正确方法是什么?

最佳答案

尝试删除await,或将其移到expectLater之外/之前。expect expectLater 之间的唯一区别是后者返回了Future,但是您忽略了该Future。expect / throwsA需要使用闭包或Future作为实际值,但是您可以在将来对await进行编码,以便在调用await ...之前抛出expectLater表达式。删除await,以便将Future传递给expectLater,然后 throwsA 将捕获并匹配该将来的错误。
因此,我建议将其更改为:

  await expectLater(employeesDB.createEmployee(employee), ...

关于dart - Dart/颤振中的单元测试异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63464412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com