RequireJs 加载模块 - Qunit [英] RequireJs loading modules - Qunit

查看:74
本文介绍了RequireJs 加载模块 - Qunit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 RequireJS 的新手,并且在将 QUnit 写入具有使用 requireJS 动态加载模块的逻辑的源代码时遇到了麻烦.

I am pretty new to RequireJS, and having trouble in writing QUnit to a source code which has logic to load modules dynamically using requireJS.

源代码如下: factory/Factory.js *

Below is the source code: factory/Factory.js *

getPage: function (callback) {

    //doSomething here

    require(['page/something'], function() {

         callback();

    })
}

在运行 QUnit 时永远不会加载模块 'page/something',并且永远不会调用回调.有什么我在这里想念的吗?感谢您的回复.

The module 'page/something' is never loaded while running QUnit, and callback is never invoked. Is there anything I'm missing here? Appreciate your response.

**QUnit factory/FactoryTests.js*

**QUnit factory/FactoryTests.js*

define(['underscore', 'factory/Factory'],
       function (_, Factory) {

           module("Factory", {
               setup:function () {
               },
               teardown:function () {
               }
           });

           test("GetPage", function () {
              var isCallbackInvoked = false;
              var mockCallback = function () {
                  isCallbackInvoked = true;
              }

              Factory.getPage(mockCallback);
              ok(isCallbackInvoked);

           });

});

*test-require-config.js**

*test-require-config.js**

require.config({

    baseUrl: "../../resources/js",

    paths:{
        jquery:'../jquery-1.8.2',
    jquery_star_rating : '../jquery/jquery.rating',
        underscore:'..underscore-1.4.1',
        backbone:'../backbone-0.9.2',
        jquery_star_rating : '../jquery.rating',
        text : '../require-text-2.0.3',
        sinon: '../../../../sinon',
    },
    shim:{
        underscore:{
            exports:'_'
        },
        backbone:{
            deps:["jquery", "underscore"],
            exports:"Backbone"
        }
        jquery_star_rating : {
            deps : ['jquery']
        }
    }

});

var dependencies = [
    'jquery',
    'jquery_star_rating',
    'underscore',
    'backbone',       
    'sinon',
];

require(dependencies, function () {

    require(['../../../../test/js/testsuite'], function(suite){

    })

});

testsuite.js

function () {

    QUnit.config.autostart = false;

    var testModules = [       
        "factory/FactoryTests.js"
    ];

    require(testModules, QUnit.start);
}());

谢谢!!

推荐答案

首先澄清一下:您的 QUnit 测试页面是什么样的?我猜它要么列出零测试,要么是一个空白页.

First, a clarification: What does your QUnit test page look like? I'm guess it's either listing zero tests, or is a blank page.

如果是这样的话,我遇到了很多麻烦事情.正确"的答案正是您正在做的事情.但是经过大量单步执行代码后,在我的设置中,尽管设置了 QUnit.config.autostart = false,但在定义任何测试之前,QUnit 仍在启动.

If that's the case, I had a lot of trouble with the same thing. The "right" answer is exactly what you are doing. But after a lot of stepping through code, in my setup, QUnit was still starting before any of my tests were defined, despite setting QUnit.config.autostart = false.

在 testsuite.js 的末尾,尝试调用 QUnit.load() 而不是 QUnit.start()(您可能可以删除 autostart =错误,也是).这是一个未记录的功能,但它是唯一对我有用的功能.我相信这是默认情况下 QUnit 附加到 onLoad 事件的功能.不幸的是,对于 RequireJS,onLoad 在大多数 js 文件加载之前触发.

At the end of testsuite.js, try calling QUnit.load() instead of QUnit.start() (you can probably drop the autostart = false, too). This is an undocumented function, but it was the only thing that worked for me. I believe it's the function QUnit attaches to the onLoad event by default. Unfortunately, with RequireJS, the onLoad triggers before most of your js files have loaded.

这篇关于RequireJs 加载模块 - Qunit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆