Chai-As-Promised即使错了也会通过

时间:2016-05-04 07:00:37

标签: javascript mocha chai chai-as-promised

const chaiAsPromised = require('chai-as-promised');
const chai = require('chai');
const expect = chai.expect;
chai.use(chaiAsPromised);

describe('[sample unit]', function() {
  it('should pass functionToTest with true input', function() {   
    expect(Promise.resolve({ foo: "bar" })).to.eventually.have.property("meh");
  });
});

这个测试通过???我正在使用" chai":" 3.5.0"," chai-as-promised":" 5.2.0",

1 个答案:

答案 0 :(得分:6)

expect(...)会返回一个承诺本身,根据测试结果,该承诺将被解析或拒绝。

对于Mocha来测试该promise的结果,你需要从测试用例中明确地返回它(这是因为Mocha有内置的promise支持):

describe('[sample unit]', function() {
  it('should pass functionToTest with true input', function() {   
    return expect(Promise.resolve({ foo: "bar" })).to.eventually.have.property("meh");
  });
});

或者,您可以使用Mocha的“常规”回调式异步设置和chai-as-promised的.notify()

describe('[sample unit]', function() {
  it('should pass functionToTest with true input', function(done) {   
    expect(Promise.resolve({ foo: "bar" })).to.eventually.have.property("meh").notify(done);
  });
});