我'm having issues getting Chai' s 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.
6 回答
你必须将一个函数传递给
expect
. 像这样:你正在这样做,你传递给
expect
调用model.get('z')
的结果 . 但是为了测试是否抛出某些东西,你必须将一个函数传递给expect
,expect
将调用它自己 . 上面使用的bind
方法创建一个新函数,调用时将调用model.get
,其中this
设置为model
,第一个参数设置为'z'
.可以找到
bind
的一个很好的解释here .作为this answer says,您也可以将代码包装在这样的匿名函数中:
如果您已经在使用ES6 / ES2015,那么您也可以使用箭头功能 . 它与使用普通匿名函数基本相同,但更短 .
这个问题有许多重复,包括没有提到Chai断言库的问题 . 以下是收集在一起的基础知识:
The assertion must call the function, instead of it evaluating immediately.
You can check for specific errors using any assertion library:
Node
Should
Chai Expect
You must handle exceptions that 'escape' the test
起初这看起来很混乱 . 就像骑自行车一样,只需点击一下就可以“点击” .
来自doc ......;的例子
因为你依赖 this context :
在 .throw 调用该函数时丢失
没有办法知道它应该是什么
你必须使用以下选项之一:
wrap 另一个函数内部的方法或函数调用
bind 上下文
另一个可能的实现,比.bind()解决方案更麻烦,但有一个有助于使expect()需要一个为覆盖函数提供
this
上下文的函数,你可以使用call()
,例如,expect(function() {model.get.call(model, 'z');}).to.throw('...');