如何使用 Q.all 和 chai-as-promised?

How does one use Q.all with chai-as-promised?

chai-as-promised 文档有以下在同一测试中处理多个承诺的示例:

it("should all be well", function (done) {
    Q.all([
        promiseA.should.become("happy"),
        promiseB.should.eventually.have.property("fun times"),
        promiseC.should.be.rejectedWith(TypeError, "only joyful types are allowed")
    ]).should.notify(done);
});

我假设这里的Q来自npm install qvar Q = require('q');

.should从哪里来?

当我尝试这个 shouldundefined 并且我得到 TypeError: Cannot call method 'notify' of undefined.

Q 是否有一些应该首先发生的猴子补丁?还是我使用了错误的版本?

我正在使用带量角器的黄瓜。据我了解,他们还不支持返回承诺,因此用户必须处理对 done.

的调用

如果我没理解错的话,Q-promise 不应该,我建议你试试这个

it("should all be well", function (done) {
    Q.all([
        promiseA.should.become("happy"),
        promiseB.should.eventually.have.property("fun times"),
        promiseC.should.be.rejectedWith(TypeError, "only joyful types are allowed")
    ]).then(done);
});

你也可以使用 require mocha-as-promised,像这样:

require("mocha-as-promised")();

it("should all be well", function (done) {
    return Q.all([
            promiseA.then(function(someData){
                //here standart chai validation;
            }),
            promiseB.then(function(someData){
                //here standart chai validation;
            });
    ]).then(done);
});

好的,您要在代码中添加下一行吗?

var chai = require("chai");
var chaiAsPromised = require("chai-as-promised");

chai.use(chaiAsPromised);

回答我自己的问题:

.should 来自 "should" 断言风格 - http://chaijs.com/guide/styles/#should。您需要 运行:

chai.should();

var Q = require('q'); 之后但在 Q.all([]).should.notify... 之前:

var Q = require('q');
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');

// ***************
chai.should();
// ***************

chai.use(chaiAsPromised);

it("should all be well", function (done) {
    Q.all([
        promiseA.should.become("happy"),
        promiseB.should.eventually.have.property("fun times"),
        promiseC.should.be.rejectedWith(TypeError, "only joyful types are allowed")
    ]).should.notify(done);
});

根据文档:

This will pass any failures of the individual promise assertions up to the test framework