一个人如何像承诺的那样把Q.all和柴一起使用呢



chai as promise文档有以下在同一测试中处理多个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的猴子补丁应该先发生?或者我用错了什么版本?

我用的是带量角器的黄瓜。据我所知,他们还不支持返回promise,所以用户必须处理对done的调用。

回答我自己的问题:

.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断言的任何失败传递给测试框架

如果我理解正确,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);
});

也可以使用required 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);

相关内容

  • 没有找到相关文章

最新更新