13

I've looked everywhere I can to find a solution to this. The only thing I've found is an unanswered post. I apologize if I've overlooked something.

The problem is that when I try to get the POST values in the /createQuestion API, the body is empty/undefined. I get errors like this Cannot read proprety 'question' of undefined coming from the API.

The Express API:

app.post("/createQuestion", function(req, res) {
    var questionType = req.body.question.type;
    var questionText = req.body.question.text;
    var questionDuringClass = req.body.question.duringClass;

    // Do a bunch of stuff

    res.send(response);
});

The test:

    var should = require('should'); 
    var assert = require('assert');
    var request = require('supertest');  
    var winston = require('winston');

    request = request('http://localhost:8080');

        describe('Questions', function() { // Test suite
            before(function(done) {
                done();
            });

        it('Should create a freeResponse question', function(done) { // Test case
        var postData = {
            "question" : {
                "type" : "freeResponse",
                "text" : "This is a test freeResponse question (automated testing)",
                "duringClass" : "1"
            }
        };

        request()
        .post('/createQuestion')
        .send(postData)
        .expect(200)
        .end(function(err, res) { // .end handles the response
            if (err) {
                return done(err);
            }

            done();
        });
    });

 it('Should delete a freeResponse question', function(done) { // Test case
        var postData = {
            "question" : {
                "type" : "freeResponse",
                "text" : "This is a test freeResponse question (automated testing)",
                "duringClass" : "1"
            }
        };

        request()
        .post('/deleteQuestion')
        .send(postData)
        .expect(200)
        .end(function(err, res) { // .end handles the response
            if (err) {
                return done(err);
            }

            done();
        });
    });

What am I missing? Is the .send() sending the POST data in some different format? Is it not POSTing it to the body of the request?

Mykola
  • 3,343
  • 6
  • 23
  • 39
Mike DeMille
  • 342
  • 1
  • 5
  • 17
  • you have request(url), where is url defined? it's an app that you include before? – alfonsodev Oct 25 '13 at 16:21
  • Oh, right. I had changed that right before I posted. I added the line towards the top to set the URL for the entire request instead of passing it to each individual test. I removed the url entries. Thanks – Mike DeMille Oct 25 '13 at 16:22
  • take in mind that done() it's only called once, the first time it's call, it finish test. – alfonsodev Oct 25 '13 at 16:23
  • can you try removing second request, ( request().post('/deleteQuestion') ) ? – alfonsodev Oct 25 '13 at 16:26
  • Right. Thanks. I removed the first done(). – Mike DeMille Oct 25 '13 at 16:27
  • that solves your problem then ? – alfonsodev Oct 25 '13 at 16:27
  • So I removed the second request, like you suggested, and it still has the same problem. req.body comes out undefined when it hits the API – Mike DeMille Oct 25 '13 at 16:28
  • Yep, you deleted first done(), but not the request. But still, the second request it may be finish before the first request has ben complete, and that could end in unexpected results. I would separate different request in different it('') sections – alfonsodev Oct 25 '13 at 16:37
  • Alright. I posted the separated code. I tried it on my system, just didn't post it. Sorry. Same results, though - empty res.body – Mike DeMille Oct 25 '13 at 16:47

1 Answers1

31

It's probably that your app is not using bodyParser middleware in place.

app.use(express.bodyParser());

From the expressjs docs:

req.body

This property is an object containing the parsed request body. This feature is provided by the bodyParser() middleware, though other body parsing middleware may follow this convention as well. This property defaults to {} when bodyParser() is used.

Here you have a complete example

var express = require('express');
var request = require('supertest');

var assert = require('assert');
var app = express();

app.use(express.bodyParser());
app.get('/', function(req, res) {
  res.send('ok');
});

app.post('/createQuestion', function(req, res) {
  var message = req.body.partA + ' ' + req.body.partB;
  res.send(message);
});

describe('testing a simple application', function() {
  it('should return code 200', function(done) {
    request(app)
      .get('/')
      .expect(200)
      .end(function(err, res){
        if(err) {
          done(err);
        } else {
          done();
        }
      });
  });

  it('should return the same sent params concatenated', function(done) {
    request(app)
      .post('/createQuestion')
      .send({ partA: 'Hello', partB: 'World'})
      .expect(200, 'Hello World')
      .end(function(err, res){
        if(err) {
          done(err);
        } else {
          done();
        }
      });
  });

});
alfonsodev
  • 2,714
  • 2
  • 23
  • 30