编写JS测试时遇到问题

时间:2014-05-13 03:43:27

标签: javascript jquery unit-testing jasmine

我正在为以下JavaScript方法编写茉莉花测试。

register: function (module, obj) {
    if (this._isString(module) && this._isObject(obj)) {
        if (!this._isMethod(obj.init)) {
            this._log(3, 'Module does not have init function');
            return;
        }
        // If modules object inside modulesData does not exist then create one
        if (typeof this.modulesData.modules === 'undefined') this.modulesData['modules'] = {};
        this.modulesData.modules[module] = obj; // Store module's object inside modulesData.modules

        if (typeof this.modulesData.$container === 'undefined') this.modulesData['$container'] = {};
        this.modulesData['$container'] = $('#' + module);

        if (obj.events && typeof obj.events === 'object') {
            CORE.AggregatedEvents.init(obj);
        }
    } else {
        this._log(3, 'Module name should be String & obj should be Object');
    }
},
    //....rest code here

我目前的测试如下

describe('Have register method', function () {
    it('CORE should have register() method', function () {
        expect(CORE.register).toEqual(jasmine.any(Function));
    });

    it('register() should accept 2 arguments', function () {
        spyOn(CORE, 'register');
        CORE.register('moduleName', {});
        expect(CORE.register).toHaveBeenCalledWith('moduleName', {});
    });

    it('register() should accept string as first and Object as second argument', function () {
        spyOn(CORE, 'register');
        CORE.register('moduleName', {});
        expect(CORE.register).toHaveBeenCalledWith(jasmine.any(String), jasmine.any(Object));
    });
});

我上面的测试测试了Core中是否存在register()方法。然后检查参数的数量和参数的类型。我的问题是如何编写Jasmine测试以测试此方法功能?

0 个答案:

没有答案