lundi 7 mars 2016

Status code tests always passing with Sails.js and Mocha

Starting with Mocha tests in Sails, every test I write seem to work fine except when I try to verify the status code of the HTTP answer. For my test:

var request = require('supertest'),
    assert = require('assert'),
    Patient = require('../api/controllers/PatientController.js');

describe('Patients module', function(){
  describe('\n##Create a patient (POST)\n', function(){

    it('POST call should return 201 when success', function(){
        request(sails.hooks.http.app)
            .post('/patient')
            .expect(301)
            .end(function(err, res){
                if (err) return done(err);
                done();
            });
    });
})

Based on the controller file:

module.exports = {
    create: function(req,res,next) {
            var params = req.params.all();
            var patientId = this.createPatientId(123);

            Patient.create({
                    _id: patientId,
                    name: 'TestName'
            }).exec(function createCB(err, updated){
                    console.log('created');
            if (err) return next(err);
            res.status(201);
            res.json(patient);
        });
    },
};

Test is always passing, no matter what status number is written in the test or in the controller. Notice in my code, test expects 301, not 201, so should fail. I've also written console.log in my controller and does not print the text.

This is the results of my tests, including some previous which are right.

Any ideas? Thanks!!

enter image description here

UPDATE

if I use done(); it throws an error done is not defined. I've done:

request(sails.hooks.http.app)
    .post('/patient')
    .expect(301);
    done();

Aucun commentaire:

Enregistrer un commentaire