我正在使用包中的 Tinytest 进行单元测试,我想测试一个方法是否引发异常,我可以使用test.throws()
.
我创建了一个流星项目:
meteor create myapp
cd myapp
meteor add tinytest
要创建一个包,我会
meteor create --package test-exception
这是我的简单测试
文件test-exception.js
Joe = {
init: function () {
throw "an exception";
}
}
文件package.js
Package.describe({
name: 'tinytest-throws',
version: '0.0.1'
});
Package.onUse(function(api) {
api.versionsFrom('1.2.0.2');
api.use('ecmascript');
api.addFiles('tinytest-throws.js');
api.export('Joe', 'server'); // create a global variable for the server side
});
Package.onTest(function(api) {
api.use('ecmascript');
api.use('tinytest');
api.use('tinytest-throws');
api.addFiles('tinytest-throws-tests.js', 'server'); // launch this test only as server
});
文件test-exception-tests.js
Tinytest.add('Call a method that raise an exception', function (test) {
test.throws(
Joe.init, // That could be a way, but this fails
"This is an exception"
);
test.throws(
Joe.init(),
"This is an exception"
);
});
有人知道如何测试异常是否被很好地提出?