user2643864 user2643864 - 6 months ago 18
AngularJS Question

How to see if a Promise contains specific data using Chai assertions

I've started using the Chakram Rest API testing framework which utilizes the Chai assertion library. In my code below Chakram.get returns a Promise. I can't seem to figure out how to see if this promise contains what I am specfically looking for. For example, Chakram.get should've got back the following string:

[
"category1",
"category2"
]


I simply want to see if it contains "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();
});
});


As you can see, I've tried the above to.contain line but get the following exception. I assume it is because Chai doesn't like the object type. Not sure if I need to convert the Promise to a different object type? If so, how?

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)

Answer

I think you are looking for response.body

expect(response.body).to.contain('category1');

here is an example with the full promise:

  it("should support sequential API interaction", function () {
    return chakram.get("url")
    .then(function (response) {
      expect(response.body).to.contain("thing");
    });
  });
Comments