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

时间:2015-03-04 13:01:45

标签: protractor q cucumberjs chai-as-promised

chai-as-promised docs在同一个测试中有以下处理多个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 qvar Q = require('q');

.should来自哪里?

当我尝试此shouldundefined时,我得到TypeError: Cannot call method 'notify' of undefined

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

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

2 个答案:

答案 0 :(得分: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声明的任何失败传递给测试框架

答案 1 :(得分: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);