【发布时间】:2015-06-29 05:13:41
【问题描述】:
我使用 unittest 库在 Dart (1.9.3) 中编写了一些带有单元测试的简单项目。我在检查构造函数是否抛出错误时遇到问题。这是我为此问题编写的示例代码:
class MyAwesomeClass {
String theKey;
MyAwesomeClass();
MyAwesomeClass.fromMap(Map someMap) {
if (!someMap.containsKey('the_key')) {
throw new Exception('Invalid object format');
}
theKey = someMap['the key'];
}
}
这里是单元测试:
test('when the object is in wrong format', () {
Map objectMap = {};
expect(new MyAwesomeClass.fromMap(objectMap), throws);
});
问题是测试失败并显示以下消息:
Test failed: Caught Exception: Invalid object format
我做错了什么?这是unittest 中的错误还是我应该使用try..catch 测试异常并检查是否引发了异常?
谢谢大家!
【问题讨论】:
-
建议重新打开,因为从“重复”主题中无法立即看出如何将其应用于构造函数。
标签: unit-testing dart