Reputation: 1073
Is it possible to test the response from NodeJs using Mocha?
A GET request is made using $http from AngularJs to NodeJs. On success of the request this function is called:
var successCallback = function (data) {
var SUCCESS_STATUS_CODE = 200;
response.set('Content-Type', 'application/json');
response.send({
statusCode: SUCCESS_STATUS_CODE,
data: data
});
};
I have tried to use Sinon to spy on the function and also Mocha to check the request but I cannot get them to work. What is the best way to write a test to check that the output from "response.send"?
Upvotes: 1
Views: 501
Reputation: 9136
To test a response of a HTTP call from Node.JS, you could use superagent
Create a folder and install inside mocha and superagent:
$ npm install superagent
$ npm install mocha
Then create a file called something like test.js with this code:
var request = require('superagent');
var assert = require('assert');
var URL = 'https://www.google.com.bo/#q=nodejs';
describe('Testing an HTTP Response', function () {
it('should have a status code 200', function (done) {
this.timeout(9000);
request
.get(URL)
.end(function (err, response) {
assert.equal(response.status, 200);
done();
});
});
});
then you can run it with mocha
$ mocha
Upvotes: 2