简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM