在stuff.js:
function init() {
return "works";
}
(function(ParentNamespace) {
ParentNamespace.MySubNamespace = {};
})(window.MyNamespace || (window.MyNamespace = {}));
在我的测试JS文件中:
/// <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");
});
使用Chutzpah Test Adapter运行上述测试时,我得到'undefined' is not an object (evaluating 'window.MyNamespace.MySubNamespace')
。也就是说,在var ns2
行上抛出异常,我从未进入上一个ok()
断言。我究竟做错了什么?在试图运行测试之前,qUnit / Chutzpah是不是已经在stuff.js中运行了代码?
答案 0 :(得分:1)
我改变了测试。以下测试工作......
/// <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");
});