QUnit没有像我期望的那样手动运行命名空间的设置代码? [英] QUnit isn't running manually namespaced setup code like I would expect?

查看:130
本文介绍了QUnit没有像我期望的那样手动运行命名空间的设置代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在stuff.js中:

In stuff.js:

function init() {
    return "works";
}

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

在我的测试JS文件中:

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

我得到'undefined'不是对象(评估'window.MyNamespace .MySubNamespace')使用Chutzpah Test Adapter运行上述测试时。也就是说,在 var ns2 行上抛出异常,我从未到达最后一个 ok()断言。我究竟做错了什么?在尝试运行测试之前,qUnit / Chutzpah不应该在stuff.js中运行代码吗?

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

这篇关于QUnit没有像我期望的那样手动运行命名空间的设置代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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