1

我无法在 nodejs 上顺利进行 jasmine 测试。我的意思是,在某些情况下,测试只是在运行过程中停止,没有通知任何内容,而是默默地错误失败。我刚才得到的一个例子是:

在 validater.validateIDs 中。有一个回调“isOkFunc”被发送到另一个模块函数到自定义 mongoose.find-function

validateIDs = function (data, dbFunc) {
        data = data || selfData;

        var
            isOkFunc = (function () {
                var ret = {};
                ret.thisData = void 0;

                ret.setData = function (data) {
                    ret.thisData = (data === undefined) ? true : data;
                };
                return ret;
            })(),
            findData = {
                playerID: data.playerID,
                gameID: data.gameID,
                turnID: data.turnID,
                objectID: data.objectID
            };

        dbFunc.find(findData, isOkFunc.setData);
        return isOkFunc.thisData;
    };

在自定义查找函数中调用回调:

this.find = function (data, cb) {
    models.Order.find(data,function (err, orders) {
        if (err) {
            console.log("error in getting orders");
            cb(void 0);
        } else {
            console.log(orders);
            cb(orders);
        }
    });
};

现在因为我忘记将 thisData 指向 ret.thisData,所以它出错了,因为变量不存在。但问题是 jasmine 并没有对此进行任何通知,它只是默默地失败并停止执行。应该如何做才能更好地调试?或者更像是对于任何实际调试应该如何?

导致问题的规范文件中的实际部分是:

isOk = validater.validateIDs(data, orderReceive);

在剥离了一些不相关的描述部分之后,这里是整个规范文件:

"use strict";
/* This is for making jasmine testing work in windows with netbeans. If this 
 * causes trouble for you, then try to find a better solution. I need this in 
 * windows enviroment */
if(process.platform.match(/^win/)) {
    require("jasmine-node");
};

var uuid = require('node-uuid'),
    orders = require("../../databases/modules/orders.js"),
    orderReceive = orders.ordersAtDatabase("../testDB"),
    data, UUID = uuid.v4();;

/* Counts the amount of finished-class received. When it reaches given point. 
 * Executes all functions. This is supposed to be executed last to remove database entries
 * 
 * Without this we can not test the database ID validation fully, since we need 
 * to have data in database for this */
var toExecuteLast = (function toExecuteLast() {
    var funcs = [], ret = {}, i,
        count = 0,
        max = 5;

    ret.add = function(func) {
        funcs.push(func);
    };
    ret.finished = function() {
        count++;
    };
    ret.do = function() {
        console.log("gogogo");
        if(count >= max) {
            console.log("gogogo2222");
            for(i = 0; i < funcs.length; i++) {
                funcs[i]();
            }
            clearInterval(toExecuteLast.do);
        }
    };

    return ret;
})();

beforeEach(function() {
    data = 
        {
            "playerID": 1,
            "gameID": 1,
            "turnID": 1,
            "objectID": UUID,
            "type": "unit",
            "action": "move",
            "exec": {
                "x": 10,
                "y": 10
            }
        };
});

describe("Testing data validation from data, that would come from frontend", function() {
    var validater;

    validater = orders.orderValidation();

    describe(". User is sending fishy data: ", function(){   
        it(". Test that everything works fine", function() {
            expect(helper).not.toThrow();
        });
        it(". Something fishy with playerID", function() {
            data.playerID = "kjdf ss";
            expect(helper).toThrow(new Error("playerID was not an integer"));
            data.playerID = 12322.33;
            expect(helper).toThrow();
        });
        it(". Something fishy with gameID", function() {
            data.gameID = "kjfkj";
            expect(helper).toThrow(new Error("gameID was not an integer"));
        });
        it(". Something fishy with turnID", function() {
            data.turnID = "| rm -r";
            expect(helper).toThrow(new Error("turnID was not an integer"));
        });
        it(". Something fishy with objectID", function() {
            data.objectID = "| rm -r";
            expect(helper).toThrow(new Error("objectID was not an UUIDv4"));
        });
        it(". Something fishy with type", function() {
            data.type = "| rm -r";
            expect(helper).toThrow();
        });
        it(". Something fishy with action", function() {
            data.action = "| rm -r";
            expect(helper).toThrow(new Error("Action contains illegal character only a-z,A-Z,0-9,_ are allowed"));
        });
        it(". Something fishy with exec", function() {
            data.exec = "| rm -r";
            expect(helper).toThrow(new Error("Action was move, but exec.x was not an integer"));
        });
        toExecuteLast.finished();
    });

    describe(". Do a database check if the IDs that are given (player, game, turn, object) are correct? ", function(){
        /* Remeber that the playerID is fetched from inner data / cookies / auth
         * so it is supposed to be correct. We need it to be correct in the 
         * database too, so we can test the game, turn and object ID validations */
        it(". Everything ok with ID validation?", function() {
            var isOk;

            isOk = validater.validateIDs(data, orderReceive);

            waitsFor(function() {
                return isOk !== 'undefined' ? true : false;
            });

            runs(function() {
                expect(isOk).not.toBeTruthy();
                isOk = false;

                data.gameID = 10;
                isOk = validater.validateIDs(data, orderReceive);

                waitsFor(function() {
                    return (isOk !== 'undefined') ? true : false;
                }, "", 1000);

                runs(function() {
                    // THIS doesn't seem to work, why?
                    expect(true).toBeTruthy();
                    toExecuteLast.finished();
                });
            });
        });

        toExecuteLast.finished();
    });
    function helper() {
        validater.validateRequest(data);
    }
});

setInterval(toExecuteLast.do, 400);
orderReceive.showAll();
//orderReceive.removeAll();
4

0 回答 0