Meteor 使用Tinytest异常进行测试
我正在一个包中使用Tinytest进行单元测试,我想测试一个方法是否引发异常,我可以使用Meteor 使用Tinytest异常进行测试,meteor,exception-handling,tinytest,meteor-packages,Meteor,Exception Handling,Tinytest,Meteor Packages,我正在一个包中使用Tinytest进行单元测试,我想测试一个方法是否引发异常,我可以使用test.throws()进行测试 我创建了一个meteor项目: meteor create myapp cd myapp meteor add tinytest 要创建一个包,我需要 meteor create --package test-exception 这是我的简单测试 文件testexception.js Joe = { init: function () { th
test.throws()
进行测试
我创建了一个meteor项目:
meteor create myapp
cd myapp
meteor add tinytest
要创建一个包,我需要
meteor create --package test-exception
这是我的简单测试
文件
testexception.js
Joe = {
init: function () {
throw "an exception";
}
}
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
});
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"
);
});
文件package.js
Joe = {
init: function () {
throw "an exception";
}
}
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
});
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"
);
});
文件测试异常tests.js
Joe = {
init: function () {
throw "an exception";
}
}
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
});
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"
);
});
有人知道如何测试异常是否被很好地提出了吗 好的,我明白了。
首先,你必须使用Meteor.Error。因此,我的Joe
对象变成:
Joe = {
init: function () {
throw new Meteor.Error("This is an exception");
}
}
现在,我可以使用Test.throws捕捉错误:
test.throws(
function() {
Joe.init()
},
"n except" // a substring of the exception message
);
try-catch
块有什么问题?我不知道是否存在test.ok()
和test.fail()
和tinytest(烹饪书中没有跟踪:),我不想在test.isTrue(true)
存在时使用test.throws()
。