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

回眸只為那壹抹淺笑 提交于 2019-12-10 21:43:10

问题


The chai-as-promised docs have the following example of dealing with multiple promises in the same test:

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

I assume that the Q here has come from npm install q and var Q = require('q');.

Where does .should come from?

When I try this should is undefined and I get TypeError: Cannot call method 'notify' of undefined.

Is there some monkey patching of Q that's supposed to happen first? Or am I using the wrong version of something?

I'm using cucumber with protractor. As I understand it they don't support returning promises yet so the user has to handle the call to done.


回答1:


Answering my own question:

.should comes from the "should" assertions style - http://chaijs.com/guide/styles/#should. You need to run:

chai.should();

after var Q = require('q'); but before 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);
});

As per the docs:

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




回答2:


if I understood correctly, Q-promise dont have should, i recommend you try this

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

also you can use require mocha-as-promised, like this:

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

Ok, are you add next line to your code?

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

chai.use(chaiAsPromised);


来源:https://stackoverflow.com/questions/28855210/how-does-one-use-q-all-with-chai-as-promised

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!