【问题标题】:qUnit testing for jQuery closureq jQuery 闭包的单元测试
【发布时间】:2019-06-10 10:29:36
【问题描述】:

我在为带有闭包的代码执行 qUnit 时遇到了麻烦。

快速概览,我正在运行 qUnit 2.5.0 和 sinon 4.3.0。

sinon 无法从 JsFileToTest_1.jsJsFileToTest_2.js 文件中检测到 iWantToTestThisOne_1() 函数。

所以现在,我无法掌握这个关闭的东西

另一件事是JsFileToTest_2.js 甚至不是闭包,而只是$(document).ready(function{});alias。还是 sinon 检测不到里面的函数。

据我所知sinons 采用来自window 对象的方法。当我检查window 对象时。 JsFileToTest_2.jsandJsFileToTest_2.js`中定义的函数没有附加到它上面。

但是,当我尝试删除 (function($) { })(jQuery);$(function() {}); 时,这些函数已经附加到 window 对象。

我的问题是,如果函数包含在闭包 (function($) { })(jQuery);$(function() {}); 中,我该如何执行 qUnit 测试。我很难理解函数是如何附加的。另外,这可能是因为我还没有完全掌握通过 sinon 存根函数的方法。

见下面的例子

JsFileToTest_1.js

(function($) {
    function iWantToTestThisOne_1() {
        //do something
        doSomething();
    }

    function iWantToTestThisOne_2() {
        //do something
        doSomething();
    }

    function iWantToTestThisOne_3() {
        //do something
        doSomething();
    }

    function doSomething() {
        //doing something
    }
})(jQuery);

JsFileToTest_2.js

$(function() {
    function iWantToTestThisOne_1() {
        //do something
        doSomething();
    }

    function iWantToTestThisOne_2() {
        //do something
        doSomething();
    }

    function iWantToTestThisOne_3() {
        //do something
        doSomething();
    }

    function doSomething() {
        //doing something
    }
});

qUnitTestCode.js

QUnit.test('iWantToTestThisOne_1()', function(assert){
    stub_doSomething = sinon.stub(window, "doSomething").returns("something");

    assert.equal(stub_doSomething.called, true, "doSomething() is called");
});

QUnit.test('iWantToTestThisOne_2()', function(assert){
    stub_doSomething = sinon.stub(window, "doSomething").returns("something");

    assert.equal(stub_doSomething.called, true, "doSomething() is called");
});

QUnit.test('iWantToTestThisOne_3()', function(assert){
    stub_doSomething = sinon.stub(window, "doSomething").returns("something");

    assert.equal(stub_doSomething.called, true, "doSomething() is called");
});

【问题讨论】:

    标签: jquery sinon qunit


    【解决方案1】:

    很遗憾,您无法使用现有代码对其进行测试。这些函数对于包装函数的范围是私有的(不能在它之外访问)。但是,您可以重新组织您的代码以允许这种情况发生:

    // module 1
    function initSomethings($) {  // <-- notice I added a name
        function iWantToTestThisOne_1() {
            //do something
            doSomething();
        }
    
        function iWantToTestThisOne_2() {
            //do something
            doSomething();
        }
    
        function iWantToTestThisOne_3() {
            //do something
            doSomething();
        }
    
        function doSomething() {
            //doing something
        }
    
        return { // give back the methods in here...
            iWantToTestThisOne_1,
            iWantToTestThisOne_2,
            iWantToTestThisOne_3,
            doSomething
            // Note that we don't have to return ALL of them, so you could keep some of them private by not returning them here
        };
    };  // <-- notice that I am NOT calling the wrapper function
    

    然后,在一些“主”模块中,我们可以初始化我们的函数:

    // "main" module
    initSomething();
    initAnother();
    

    这会处理您的应用程序代码。为了测试,我们可以使用所有函数的返回值:

    QUnit.test('iWantToTestThisOne_1()', function(assert){
        // Start by initializing your functions...
        let methods = initSomething();
        // Then stub it out...
        let stub_doSomething = sinon.stub(methods, "doSomething").returns("something");
    
        // Now you call the function you're testing:
        methods.iWantToTestThisOne_1();
        // And do your assertions
        assert.equal(stub_doSomething.called, true, "doSomething() is called");
        // more assertions...
    });
    

    【讨论】:

    • 基本上我必须修改目标源代码才能测试对吗?我忘了提到目标源是遗留代码,在任何情况下都不能修改代码。无论如何,我明白了你的意思,如果不重构源代码,就无法进行 qunit 。我可能会用这个特定的实例取消 qunit
    • 我明白了。好吧,要清楚,这不是 QUnit 的问题。您也无法使用任何其他测试框架进行测试。这些内部函数在该闭包之外无法访问。
    • 啊是的,当我说取消 qunit 时,我的意思是我将进行手动测试而不使用任何测试框架。
    猜你喜欢
    • 2016-01-26
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 2016-06-01
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多