我已经开始使用Chakram测试框架,它使用了柴断言库。在我下面的代码中,Chakram.get返回一个承诺。我似乎想不出该如何看这个承诺是否包含了我想要的东西。例如,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行,但是得到了下面的异常。我想是因为柴不喜欢对象类型。不确定是否需要将承诺转换为不同的对象类型?如果是这样的话,是怎么做的?
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)发布于 2016-05-26 17:58:34
我想你是在找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");
});
});发布于 2018-03-17 19:48:41
您可以对response.body进行字符串化,然后使用函数索引来检查它是否包含您所期望的内容。
https://stackoverflow.com/questions/37467901
复制相似问题