首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用柴断言查看承诺是否包含特定数据

如何使用柴断言查看承诺是否包含特定数据
EN

Stack Overflow用户
提问于 2016-05-26 17:53:45
回答 2查看 1.4K关注 0票数 1

我已经开始使用Chakram测试框架,它使用了柴断言库。在我下面的代码中,Chakram.get返回一个承诺。我似乎想不出该如何看这个承诺是否包含了我想要的东西。例如,Chakram.get应该得到以下字符串:

代码语言:javascript
复制
[
 "category1",
 "category2"
]

我只想看看它是否包含"category1“。

代码语言:javascript
复制
var chakram = require('chakram'),
    expect = chakram.expect;

process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';

describe("Categories", function() {
    it("should return the list of categories for devices that are installed in the project", function () {
        var response = chakram.get("https://192.168.2.2/category");
        expect(response).to.have.status(200);
        expect(response).not.to.have.header('non-existing-header');
        expect(response).to.contain('category1');
        return chakram.wait();
    });
});

正如您所看到的,我尝试了上面的to.contain行,但是得到了下面的异常。我想是因为柴不喜欢对象类型。不确定是否需要将承诺转换为不同的对象类型?如果是这样的话,是怎么做的?

代码语言:javascript
复制
TypeError: obj.indexOf is not a function
  at include (/Users/acme/node_modules/chai/lib/chai/core/assertions.js:228:45)
  at /Users/acme/node_modules/chakram/node_modules/chai-as-promised/lib/chai-as-promised.js:304:26
  at _fulfilled (/Users/acme/node_modules/chakram/node_modules/q/q.js:834:54)
  at self.promiseDispatch.done (/Users/acme/node_modules/chakram/node_modules/q/q.js:863:30)
  at Promise.promise.promiseDispatch (/Users/acme/node_modules/chakram/node_modules/q/q.js:796:13)
  at /Users/acme/node_modules/chakram/node_modules/q/q.js:604:44
  at runSingle (/Users/acme/node_modules/chakram/node_modules/q/q.js:137:13)
  at flush (/Users/acme/node_modules/chakram/node_modules/q/q.js:125:13)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-26 17:58:34

我想你是在找response.body

代码语言:javascript
复制
expect(response.body).to.contain('category1');

下面是一个完整承诺的例子:

代码语言:javascript
复制
  it("should support sequential API interaction", function () {
    return chakram.get("url")
    .then(function (response) {
      expect(response.body).to.contain("thing");
    });
  });
票数 2
EN

Stack Overflow用户

发布于 2018-03-17 19:48:41

您可以对response.body进行字符串化,然后使用函数索引来检查它是否包含您所期望的内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37467901

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档