3

chai-as-promised 文档有以下在同一个测试中处理多个 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")
    ]).should.notify(done);
});

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

从哪里来.should

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

Q是否应该先进行一些猴子修补?还是我使用了错误的版本?

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

4

2 回答 2

3

回答我自己的问题:

.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);
});

根据文档:

这会将单个 Promise 断言的任何失败传递给测试框架

于 2015-03-04T13:16:29.833 回答
0

如果我理解正确,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);
于 2015-03-04T13:07:09.183 回答