Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Express testing mock res.status(status).json(obj)

I get the following error when trying to test my method:

TypeError: Cannot call method 'json' of undefined

Below is my code, i'd get the same error for 'status' if i remove res.status from the test method.

How do i define 'json' so i dont get an exception thrown at:

res.status(404).json(error);

when testing this function.

stores.js

{ //the get function declared above (removed to ease of reading)
        // using a queryBuilder
        var query = Stores.find();
        query.sort('storeName');
        query.exec(function (err, results) {
            if (err)
                res.send(err);
            if (_.isEmpty(results)) {
                var error = {
                    message: "No Results",
                    errorKey: "XXX"
                }
                res.status(404).json(error);
                return;
            }
            return res.json(results);
        });
    }

storesTest.js

it('should on get call of stores, return a error', function () {

    var mockFind = {
        sort: function(sortOrder) {
            return this;
        },
        exec: function (callback) {
            callback('Error');
        }
    };

    Stores.get.should.be.a["function"];

    // Set up variables
    var req,res;
    req = {query: function(){}};
    res = {
        send: function(){},
        json: function(err){
            console.log("\n : " + err);
        },
        status: function(responseStatus) {
            assert.equal(responseStatus, 404);
        }
    };

    StoresModel.find = sinon.stub().returns(mockFind);

    Stores.get(req,res);
like image 468
myguy1221 Avatar asked Jan 20 '15 19:01

myguy1221


1 Answers

The status method happens to be a chainable method. The convention for chainable methods is to always return this at the end. In your tests, you mock up the res.status method but forgot to return this;.

res = {
    send: function(){ },
    json: function(err){
        console.log("\n : " + err);
    },
    status: function(responseStatus) {
        assert.equal(responseStatus, 404);
        // This next line makes it chainable
        return this; 
    }
}
like image 179
Ryan Wheale Avatar answered Sep 22 '22 13:09

Ryan Wheale