Chai-As-Promised 即使错误也会通过
Chai-As-Promised passes even when it is wrong
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",
expect(...)
returnpromise 本身,将根据测试解决或拒绝。
要让 Mocha 测试该 promise 的结果,您需要从测试用例中显式 return 它(这是有效的,因为 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 的 "regular" 回调式异步设置和 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);
});
});
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",
expect(...)
returnpromise 本身,将根据测试解决或拒绝。
要让 Mocha 测试该 promise 的结果,您需要从测试用例中显式 return 它(这是有效的,因为 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 的 "regular" 回调式异步设置和 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);
});
});