dor*_*emi 238 javascript mocha.js node.js chai
我有问题让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();
});
Run Code Online (Sandbox Code Playgroud)
失败:
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.
Run Code Online (Sandbox Code Playgroud)
Lou*_*uis 318
你必须传递一个功能expect.像这样:
expect(model.get.bind(model, 'z')).to.throw('Property does not exist in model schema.');
expect(model.get.bind(model, 'z')).to.throw(new Error('Property does not exist in model schema.'));
Run Code Online (Sandbox Code Playgroud)
你正在这样做,你正在转移到调用expect的结果model.get('z').但是为了测试是否抛出了某些东西,你必须将一个函数传递给expect它,expect它将调用自己.在bind上面使用方法创建一个其中要求将调用时新功能model.get与this集到的值model,并设置为第一个参数'z'.
bind可以在这里找到一个很好的解释.
twi*_*wiz 165
正如这个答案所说,你也可以将代码包装在一个匿名函数中,如下所示:
expect(function(){
model.get('z');
}).to.throw('Property does not exist in model schema.');
Run Code Online (Sandbox Code Playgroud)
Dan*_* T. 83
如果您已经在使用ES6/ES2015,那么您也可以使用箭头功能.它与使用普通匿名函数基本相同,但更短.
expect(() => model.get('z')).to.throw('Property does not exist in model schema.');
Run Code Online (Sandbox Code Playgroud)
Cha*_*iam 70
这个问题有许多重复,包括没有提到Chai断言库的问题.以下是收集在一起的基础知识:
断言必须调用函数,而不是立即进行评估.
assert.throws(x.y.z);
// FAIL. x.y.z throws an exception, which immediately exits the
// enclosing block, so assert.throw() not called.
assert.throws(()=>x.y.z);
// assert.throw() is called with a function, which only throws
// when assert.throw executes the function.
assert.throws(function () { x.y.z });
// if you cannot use ES6 at work
function badReference() { x.y.z }; assert.throws(badReference);
// for the verbose
assert.throws(()=>model.get(z));
// the specific example given.
homegrownAssertThrows(model.get, z);
// a style common in Python, but not in JavaScript
Run Code Online (Sandbox Code Playgroud)
您可以使用任何断言库检查特定错误:
assert.throws(() => x.y.z);
assert.throws(() => x.y.z, ReferenceError);
assert.throws(() => x.y.z, ReferenceError, /is not defined/);
assert.throws(() => x.y.z, /is not defined/);
assert.doesNotThrow(() => 42);
assert.throws(() => x.y.z, Error);
assert.throws(() => model.get.z, /Property does not exist in model schema./)
Run Code Online (Sandbox Code Playgroud)
should.throws(() => x.y.z);
should.throws(() => x.y.z, ReferenceError);
should.throws(() => x.y.z, ReferenceError, /is not defined/);
should.throws(() => x.y.z, /is not defined/);
should.doesNotThrow(() => 42);
should.throws(() => x.y.z, Error);
should.throws(() => model.get.z, /Property does not exist in model schema./)
Run Code Online (Sandbox Code Playgroud)
expect(() => x.y.z).to.throw();
expect(() => x.y.z).to.throw(ReferenceError);
expect(() => x.y.z).to.throw(ReferenceError, /is not defined/);
expect(() => x.y.z).to.throw(/is not defined/);
expect(() => 42).not.to.throw();
expect(() => x.y.z).to.throw(Error);
expect(() => model.get.z).to.throw(/Property does not exist in model schema./);
Run Code Online (Sandbox Code Playgroud)
您必须处理"逃避"测试的异常
it('should handle escaped errors', function () {
try {
expect(() => x.y.z).not.to.throw(RangeError);
} catch (err) {
expect(err).to.be.a(ReferenceError);
}
});
Run Code Online (Sandbox Code Playgroud)
起初这看起来很混乱.就像骑自行车一样,只需点击一下就可以"点击".
doc中的示例...;)
因为您依赖于此上下文:
您必须使用以下选项之一:
绑定上下文
// wrap the method or function call inside of another function
expect(function () { cat.meow(); }).to.throw(); // Function expression
expect(() => cat.meow()).to.throw(); // ES6 arrow function
// bind the context
expect(cat.meow.bind(cat)).to.throw(); // Bind
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
126916 次 |
| 最近记录: |