1

nodejs でジャスミンのテストがスムーズに動作しません。つまり、テストが実行の途中で停止し、何も通知せず、黙ってエラーで失敗する状況があるということです。少し前に得た一例は次のとおりです。

validater.validateIDs で。カスタムmongoose.find-functionへの別のモジュール関数に送信されるコールバック「isOkFunc」があります

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