QUnit isn't running manually namespaced setup code like I would expect?

时光怂恿深爱的人放手 提交于 2019-12-02 19:23:38

问题


In stuff.js:

function init() {
    return "works";
}

(function(ParentNamespace) {
    ParentNamespace.MySubNamespace = {};
})(window.MyNamespace || (window.MyNamespace = {}));

In my test JS file:

/// <reference path="../../../project1/Shared/sub1/Javascript/stuff.js" />
test("foo test", function () {
    deepEqual(init(), "works", "couldn't access source JS file");
    ok(window, "no window context");
    var ns = window.MyNamespace;
    ok(ns in window, "namespace is bad");
    var ns2 = window.MyNamespace.MySubNamespace;
    ok(ns2 in window, "subnamespace is bad");
});

I get 'undefined' is not an object (evaluating 'window.MyNamespace.MySubNamespace') when running the above test using Chutzpah Test Adapter. That is to say, an exception is thrown on the var ns2 line, and I never get to the last ok() assertion. What am I doing wrong? Shouldn't qUnit/Chutzpah have run the code in stuff.js before trying to run the test?


回答1:


I have changed the test. Following test works...

/// <reference path="../../../project1/Shared/sub1/Javascript/stuff.js" />
test("foo test", function () {
deepEqual(init(), "works", "couldn't access source JS file");
ok(window, "no window context");    
ok('MyNamespace' in window, "namespace is bad");    
ok('MySubNamespace' in window.MyNamespace, "subnamespace is bad");
});


来源:https://stackoverflow.com/questions/14572919/qunit-isnt-running-manually-namespaced-setup-code-like-i-would-expect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!