Jasmine如何处理错误

时间:2013-11-08 13:56:38

标签: javascript node.js debugging jasmine

我无法在nodejs上顺利进行茉莉花测试。我的意思是,有些情况下测试只是在运行过程中停止,没有通知任何东西,但是默默地失败了。我刚才得到的一个例子是:

在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,那就错了,因为变量不存在。但问题是茉莉花没有通知任何关于此的事情,它只是默默地失败并停止执行。如何更好地进行调试呢?或者更像是如何进行实际调试?

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

isOk = validater.validateIDs(data, orderReceive);

剥离几个不相关的descibe-sections后,这是整个spec-file:

"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();

0 个答案:

没有答案