QUnit isn't running manually namespaced setup

2019-03-06 14:41发布

问题:

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");
});