如何使用 rejectedWith 执行错误消息的精确匹配?

How can I use rejectedWith to perform an exact match of the error message?

我最近选择了 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 柴@4.0.2 chai-as-promised@7.1.1 q@1.5.0

非常感谢。

Is this the default behaviour?

是的,是的。 Chai-as-promised 反映了 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" 通过。

要提供使用正则表达式的替代方法,可以执行此操作以强制执行完全匹配:

it('should fail with an error message "foo".', ()=>{
    return expect(fn).to.eventually.be.rejected.and.has.property('messa‌​ge', 'foo');
});

这是检查被拒绝的对象是否有属性 message "foo".

请注意:这是为了从 Promise 断言错误 rejected/throw,所以这里需要 eventually,否则它不会 运行 正确。