Not able to fetch object value in nodeunit test cases for server-side code testing from one method to another

142 Views Asked by At

I'm using nodeunit for node.js server-side code testing. Here is my code snippet. I have not added all the codes but you can view the error reference.

Here in the bwlow code in test adding a project method I am adding project to mongodb and then once I got the response from server after saved I am assigning the newly added _id to me.projectToUpdate['_id'].

My problem is I'm not able to get the value of me.projectToUpdate['_id'] in test build a project test case.

exports.group = {
    setUp: function(callback) {
        var me = this;
        me.projectToSave = {};
        me.projectToUpdate = {};
    },

    "test for smoke": function(test) {
        test.ok(null == null);
        test.done();
    },

    "test adding a project": function(test) {
        var me = this;

        me.queue.call("Adding project to DB", function(callbacks) {
            // Here I have defined the me.projectToSave object with all fields

            project.save(me.projectToSave, callbacks.add(function(call, callReturn) {
                //Here I am assigning the project id to me.projectToUpdate object
                me.projectToUpdate['_id'] = String(callReturn._id);
            }));
        });
        me.queue.process(test);
    },

    "test build a project": function(test) {
        var me = this;

        me.queue.call("Let start the build process", function(callbacks) {
            var command = me.config['xmlDeploy'] + ' ' + me.config['options'];
            // Here I need the me.projectToUpdate['_id'] to send to server
            build.start({project: me.projectToUpdate['_id']}, callbacks.add(function() {
            }));
        });

        me.queue.process(test);
    }
};
1

There are 1 best solutions below

1
On

It's probably a scoping issue

setUp: function(callback) {
    var me = this;
    me.projectToSave = {};
    me.projectToUpdate = {};
},

I would recommend just moving the state outside the test

var me = {}
me.projectToSave = {};
me.projectToUpdate = {};

exports.group = {
}