【发布时间】:2014-03-02 11:12:10
【问题描述】:
我无法让 Chai 的 expect.to.throw 在我的 node.js 应用程序的测试中工作。测试在抛出的错误上一直失败,但是如果我将测试用例包装在 try 中并捕获并断言捕获的错误,它就可以工作。
expect.to.throw 是否不像我认为的那样工作?
it('should throw an error if you try to get an undefined property', function (done) {
var params = { a: 'test', b: 'test', c: 'test' };
var model = new TestModel(MOCK_REQUEST, params);
// neither of these work
expect(model.get('z')).to.throw('Property does not exist in model schema.');
expect(model.get('z')).to.throw(new Error('Property does not exist in model schema.'));
// this works
try {
model.get('z');
}
catch(err) {
expect(err).to.eql(new Error('Property does not exist in model schema.'));
}
done();
});
失败:
19 passing (25ms)
1 failing
1) Model Base should throw an error if you try to get an undefined property:
Error: Property does not exist in model schema.
【问题讨论】:
标签: javascript node.js mocha.js chai