0

I have a promise which checks if I'm authorized or not (returning true or false). Within that promise, when authorization is false, I'm also adding the 403 statusCode to the response object that I pass as a parameter. I'm testing the result of my promise with chai-as-promised but I didn't manage to find a way to also test the status code of the response after the promise is resolved.

var result = authorizePromise(request, response);
return expect(result).to.eventually.equal(false).and(response.statusCode.should.be(403));
Veve
  • 6,643
  • 5
  • 39
  • 58
Bianca
  • 382
  • 4
  • 12

1 Answers1

1

the response should be used in parent scope.
and the assertion should be called after the promise is ready
so it should be a function that called after.
your code example .and(response.statusCode.should.be(403)); runs the assertion instant, and passes it as parameter to .and() it does not wait for the promise.

*You can use .andinstead of .then too.

var response = {};
var resultPromise = authorizePromise(request, response);
return expect(resultPromise).to.eventually.equal(false)
    .then(function(){return response.statusCode.should.be(403)})
ya_dimon
  • 3,483
  • 3
  • 31
  • 42
  • Thank you! I didn't think about using .then to make sure that my promise is resolved when checking the result. – Bianca Mar 04 '16 at 15:13