0
votes

I'm having a problem with Mocha tests on my Express app. I have a REST API set up that I know works in the browser, but it's getting a bit large and therefore manual testing has become tedious.

Anyway, all my GET tests work just fine, but when I add tests for my POST requests, they fail:

 Uncaught AssertionError: expected { Object (domain, _events, ...) } to have status code 200 but got 400
  at testPostSingle (test-app.js:297:21)
  at test-app.js:195:21
  at Test.Request.callback (/home/jacobd/healthboard/node_modules/chai-http/node_modules/superagent/lib/node/index.js:603:3)
  at Stream.<anonymous> (/home/jacobd/healthboard/node_modules/chai-http/node_modules/superagent/lib/node/index.js:767:18)
  at Unzip.<anonymous> (/home/jacobd/healthboard/node_modules/chai-http/node_modules/superagent/lib/node/utils.js:108:12)
  at _stream_readable.js:944:16

I'm using Mocha, with the Chai should library and chai-http for requests.

My relevant code:

models.forEach(function(model, i) {

    var url = '/api/v1/' + model;
    var list = lists[i];

    /****************************** API POST TESTS ******************************/

    describe(util.format('API: /%s POST', model), function() {
        var minArgsObj = {
            title: 'Test ' + model + ' Title'
        };

        // Initialize list at start
        before(function(done) {
            instances._init(function(err) {
                if (!err) done();
            });
        });

        // Nuke list before each test
        beforeEach(function(done) {
            list.clear();
            done();
        });

        // Make sure POST single object works
        it('should create and add model on ' + url + ' with minimal arguments', function(done) {
            var req = {};
            req.options = _.clone(minArgsObj);
            chai.request('server')
                .post(url)
                .send(req)
                .end(function(err, res) {
                    testPostSingle(res, minArgsObj);
                    list.list.length.should.equal(1);
                    res.body.should.eql(list.list[0]);
                    done();
                });
        });
    });
});


function testPostSingle(res, ref) {
    res.should.have.status(200);
    ...
}

When I put a log message in the POST route declaration, it doesn't show up, so that tells me that my request is getting stopped before even hitting my server. Maybe the route isn't getting mounted properly in Mocha? It works when I make the request outside of Mocha, but I can't figure out why it won't work in a test environment.

Thanks in advance for your help, and let me know if you need any more info!

2

2 Answers

0
votes

The relevant code is in testPostSingle. Your call is returning 400 bad request. Are you sure the route is correct or is set up to handle POST as well as GET? Are you sure parameters are correct? Make testPostSingle print out the body of the HTTP response so you know the details. You can also add some debug code in the route for that request on your server.

0
votes

I'm terrible and issue is very simple:

Instead of chai.request('server')

It needs to be chai.request(server)

Thanks to Jason Livesay for pointing me at the right line!