[英]How to see if a Promise contains specific data using Chai assertions
我已经开始使用Chakram Rest API测试框架,该框架利用了Chai断言库。 在下面的代码中,Chakram.get返回一个Promise。 我似乎无法弄清楚如何看待这个承诺是否包含我正在寻找的东西。 例如,Chakram.get应该返回以下字符串:
[
"category1",
"category2"
]
我只是想看看它是否包含“ category1”。
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行,但是得到了以下异常。 我猜想是因为Chai不喜欢对象类型。 不确定是否需要将Promise转换为其他对象类型? 如果是这样,怎么办?
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)
我认为您正在寻找response.body
expect(response.body).to.contain('category1');
这是一个充满希望的例子:
it("should support sequential API interaction", function () {
return chakram.get("url")
.then(function (response) {
expect(response.body).to.contain("thing");
});
});
您可以将response.body字符串化,然后使用功能索引检查其是否包含您所期望的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.