使用Tinytest异常进行测试

时间:2015-10-13 13:35:28

标签: meteor exception-handling tinytest meteor-packages

我正在使用包中的Tinytest进行单元测试,我想测试一个方法引发异常,我可以使用<client> <endpoint address="https://xxx.yyy.com:10803" behaviorConfiguration="clientEndpointCredential" binding="basicHttpBinding" bindingConfiguration="bsapi_binding" contract="CriteriaList.bsapi_port" name="bsapi_port" /> <endpoint address="https://xxx.yyy.com:10803" behaviorConfiguration="clientEndpointCredential" binding="basicHttpBinding" bindingConfiguration="bsapi_binding" contract="ContainerList.bsapi_port" name="bsapi_port" /> <endpoint address="https://xxx.yyy.com:10803" behaviorConfiguration="clientEndpointCredential" binding="basicHttpBinding" bindingConfiguration="bsapi_binding" contract="DisplayUnitList.bsapi_port" name="bsapi_port" /> <endpoint address="https://xxx.yyy.com:10803" behaviorConfiguration="clientEndpointCredential" binding="basicHttpBinding" bindingConfiguration="bsapi_binding" contract="DomainList.bsapi_port" name="bsapi_port" /> <endpoint address="https://xxx.yyy.com:10803" behaviorConfiguration="clientEndpointCredential" binding="basicHttpBinding" bindingConfiguration="bsapi_binding" contract="ResourceCriteriaAdd.bsapi_port" name="bsapi_port" /> <endpoint address="https://xxx.yyy.com:10803" behaviorConfiguration="clientEndpointCredential" binding="basicHttpBinding" bindingConfiguration="bsapi_binding" contract="ResourceCriteriaUpdate.bsapi_port" name="bsapi_port" /> <endpoint address="https://xxx.yyy.com:10803" behaviorConfiguration="clientEndpointCredential" binding="basicHttpBinding" bindingConfiguration="bsapi_binding" contract="ResourceCriteriaList.bsapi_port" name="bsapi_port" /> <endpoint address="https://xxx.yyy.com:10803" behaviorConfiguration="clientEndpointCredential" binding="basicHttpBinding" bindingConfiguration="bsapi_binding" contract="ResourceCriteriaListByCriteriaId.bsapi_port" name="bsapi_port" /> </client> 进行测试。

我创建了一个流星项目:

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

有人知道如何测试异常是否被提升?

1 个答案:

答案 0 :(得分:1)

好的,我明白了。 首先,您必须使用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
);