如何将 Q.all 与 chai-as-promised 一起使用? [英] How does one use Q.all with chai-as-promised?

查看:70
本文介绍了如何将 Q.all 与 chai-as-promised 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

it("应该一切顺利", function (done) {Q.all([promiseA.should.become("happy"),promiseB.should.eventually.have.property(欢乐时光"),promiseC.should.be.rejectedWith(TypeError, "只允许快乐类型")]).should.notify(done);});

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

.should 来自哪里?

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

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

我正在使用带有量角器的黄瓜.据我了解,他们尚不支持返回承诺,因此用户必须处理对 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.use(chaiAsPromised);它(应该一切都好",功能(完成){Q.all([promiseA.should.become("happy"),promiseB.should.eventually.have.property(欢乐时光"),promiseC.should.be.rejectedWith(TypeError, "只允许快乐类型")]).should.notify(done);});

根据文档:

<块引用>

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

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.

解决方案

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

这篇关于如何将 Q.all 与 chai-as-promised 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆