使用QUnit测试RequireJS模块

时间:2014-06-11 15:28:07

标签: javascript jquery requirejs qunit

我正在尝试测试一个有两个依赖项的requirejs模块(jquery和另一个自定义模块)。

Mymodule中-Test.js

'use strict';

(function() {

    var uut,
        modulePath= "../../main/webapp/js/modules/myModule.js";


    module("myModule object test suite", {
        setup: function() {
            QUnit.stop();

            require.config({
                map: {
                  "*": {
                     "jquery": "../../main/webapp/js/jquery/jquery-1.11.0.min.js",
                     "screenLabelsResolver": "../../main/webapp/js/modules/my-screen-labels-resolver"
                  }
                }
            });
            require([modulePath], function(module) {
                uut = module;
                QUnit.start();
            });
        },
        teardown: function() {
            require.undef(modulePath);
            require.config({
                map: {
                  "*": {
                     "jquery": "jquery",
                      "screenLabelsResolver": "../../main/webapp/js/modules/my-screen-labels-resolver"
                  }
                }
            });
        }
    });

    test("Given A Page I Expect The Global myModule Object To Exist", function() {
        ok( uut !== undefined );
    });



}());

我使用require.config通过stop()和Start()传递依赖项。

myModule.js

'use strict';

define(["jquery", "screenLabelsResolver"], function($, screenLabelsResolver) {

    var metaTag = $("meta[name='application-name']"),
        currentBrand = metaTag.attr("data-brand"),
        currentWidth,
        viewState,
        sessionTimeoutValue = metaTag.attr("data-sessiontimeoutvalue"),
        sessionTimeoutWarningValue = metaTag.attr("data-sessiontimeoutwarningvalue"),
        screenLabels = {},
        perceptionDate = metaTag.attr("data-todayatmidnight"),
        currentViewportWidth = $(window).width(),
        isViewState = metaTag.attr("data-isviewstate"),
        isTouch = $("html").hasClass("touch")

    return {
        metaTag: function () {
            return metaTag;
        },
        currentBrand: function(){
            return currentBrand;
        },
        currentViewportWidth: function(){
            return currentViewportWidth;
        },
        isViewState: function(){
            return isViewState;
        },
        sessionTimeoutValue: function(){
            return sessionTimeoutValue;
        },
        sessionTimeoutWarningValue: function(){
            return sessionTimeoutWarningValue;
        },
        getPerceptionDate: function(){
            return perceptionDate;
        },
        getOrientation: function () {
            return ( window.orientation == -90 || window.orientation == 90 ) ? "landscape" : "portrait";
        },
        isTouch: function(){
            return isTouch;
        },
        screenLabels: function() {
            if (screenLabels = {}) {
                screenLabels = screenLabelsResolver( metaTag.attr("data-viewstate") /* or however you want to get the current viewstate name */ );
            }
            return screenLabels;
        }
    };
});

我收到错误“Uncaught TypeError: undefined is not a function”,我尝试在行var metaTag = $("meta[name='application-name']")中使用jQuery($)。

不知何故,jQuery在调用时没有正确加载。

我的问题是,这是测试具有多个依赖项的r.js模块的正确方法吗?如果是这样,上述代码中的基本错误是什么?

非常感谢提前。

0 个答案:

没有答案