1

我正在使用 nodeunit 进行 node.js 服务器端代码测试。这是我的代码片段。我没有添加所有代码,但您可以查看错误参考。

在方法中的 bwlow 代码中,test adding a project我将项目添加到 mongodb,然后在保存后从服务器获得响应后,我将新添加的 _id 分配给 me.projectToUpdate['_id']。

我的问题是我无法在test build a project测试用例中获取 me.projectToUpdate['_id'] 的值。

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);
    }
};
4

1 回答 1

0

这可能是一个范围问题

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

我建议将状态移到测试之外

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

exports.group = {
}
于 2013-08-13T10:50:04.467 回答