我最近选择了 JS 单元测试库 Mocha、Chai 和 Chai-As-Promise。但是,我遇到了一种情况,我不确定这是默认行为还是我错过了。
当我断言一条错误消息被 promise 拒绝时,似乎只要预期的错误消息是实际错误消息的子字符串,断言就会通过。下面是一个例子:
var chai = require('chai');
var expect = chai.expect;
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
var q = require('q');
describe('demo test', function(){
// a mock up promise function for demo purpose
function test(input){
var d = q.defer();
setTimeout(function() {
if(input){
d.resolve('12345');
}else{
// throw a new Error after half a sec here
d.reject(new Error('abcde fghij'));
}
}, (500));
return d.promise;
}
// assertion starts here
it('should pass if input is true', ()=>{
return expect(test(true)).to.eventually.equal('12345');
});
it('this passes when matching the first half', ()=>{
return expect(test(false)).to.be.rejectedWith('abcde');
});
it('this also passes when matching the second half', ()=>{
return expect(test(false)).to.be.rejectedWith('fghij');
});
it('this again passes when even only matching the middle', ()=>{
return expect(test(false)).to.be.rejectedWith('de fg');
});
it('this fails when the expected string is not a substring of the actual string', ()=>{
return expect(test(false)).to.be.rejectedWith('abcdefghij');
});
});
这是默认行为吗?如果是,是否可以选择强制完全匹配错误消息?
Mocha @3.4.2 Chai @4.0.2 chai-as-promised@7.1.1 q@1.5.0
非常感谢。
最佳答案
Is this the default behaviour?
是的,是的。 Chai-as-promised 反射(reflect)了 Chai 所做的事情。当您使用 expect(fn).to.throw("foo")
时,Chai 会在错误消息中查找子字符串 foo
。如果 Chai-as-promised 的工作方式不同,那将会令人困惑。
If it is, is there an option to force an exact match of the error message?
没有可设置的选项。但是,您可以传递正则表达式而不是字符串。如果您使用 /^abcde$/
进行测试,错误消息将必须 完全 "abcde"
才能通过.
关于javascript - 如何使用 rejectedWith 执行错误消息的精确匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45227354/