i am doing unit testing in Node js with Express js and for testing i am using mocha and for mocking of data i am using sinon. Everything is fine but my problem is when i run the test case if the it() contains multiple assertions and anyone of them got failed then the mocha shows that the whole it() is failed. But i want the other assertion to passed even though any one fails. I don't want to write one it() for each field. My test code is
//loading testing dependencies
var request = require('supertest');
var server = require('./app');
var chai = require('chai');
var chaiHttp = require('chai-http');
var sinon = require("sinon");
var should = chai.should();
//configuring chai
chai.use(chaiHttp);
//ORM controller (we need to mock data in it's method)
var rootController = require('./app/controllers/users/users_controller');
//Writing test cases
describe('loading express', function () {
//mock data before each request
before(function(){
//select the method of the ORM controller which you want to mock
sinon.stub(rootController, "get", //get is the method of ORM's customers_controller'
function(req, res, next){
//response object which we are going to mock
var response = {};
response.status = 'success',
response.data = {
userId: '0987654321@ef',
userName:'John'
};
next(response);
});
});
it('responds to /users/getUserData', function testMethod(done) {
//call server file (app.js)
request(server)
//send request to the Express route which you want to test
.get('/users/getUserData?id=0987654321')
//write all expactions here
.expect(200)
.end(function(err, res){
console.log("Generated response is ", res.body);
res.should.have.status(200);
res.body.should.be.a('object');
//res.body.status.should.equal("success");
res.body.data.userId.should.equal("0987654321@ef347389");
res.body.data.userName.should.equal("John");
//done is the callback of mocha framework
done();
});
});
it('responds to /', function testSlash(done) {
request(server)
.get('/')
.expect(200, done);
});
it('404 everything else', function testPath(done) {
request(server)
.get('/foo/bar')
.expect(404, done)
});
});
You can see here my userId should be failed and userName should be passed but when i run this code it says that responds to /users/getCustomerData got failed. Instead of it mocha should say that userId field got failed and userName field got passed.