vendredi 1 avril 2016

node.js / express / mocha testing post request issue

I built an app using node.js + expressjs and am using mocha to test my post request. My test passes although my post request does not touch api since my api's console.logs do not print out.

My API:

api.post('/api/addreport', function(req, res) {
    console.log('add report hit..'); //this does not print
    console.log(req.body); //this does not print
    res.sendStatus(200);
});

My test:

var express = require('express');
var app = express();
var chai = require('chai');
var expect = require('chai').expect;
var should = require('should');
var supertest = require('supertest');
var server = supertest.agent("https://localhost:3001");
var request = require('supertest');
var bodyParser = require('body-parser');


//Body parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

it("should add report", function(done){
request('https://localhost:3001')
.post('/api/addreport/')
.send(data)
.expect(200)
.end(function(err,res){
  done();
});
});

running mocha ajax results in:

✓ should add report

1 passing (34ms)

My data:

var data = {
    report_id: 'abc123' + Math.random(10),
    project_code: 'test_project_code',
    startDate: '2016-01-01',
    endDate: '2016-01-15',
};

Can someone help?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire