代码之家  ›  专栏  ›  技术社区  ›  lemoncodes

jQuery闭包的qUnit测试

  •  0
  • lemoncodes  · 技术社区  · 7 年前

    对于带有闭包的代码,我在执行qUnit时遇到问题。

    快速概述,我运行的是Qunit2.5.0和sinon 4.3.0。

    sinon 无法检测 iWantToTestThisOne_1() 两者的功能 JsFileToTest_1.js JsFileToTest_2.js 文件夹。

    另一件事是 JsFileToTest_2.js文件 alias 属于 $(document).ready(function{});

    据我所知 sinons 方法来自 window 反对。当我检查 反对。中定义的函数 JsFileToTest_2 .js公司 and JsFileToTest_2.js`未附加到它。

    (function($) { })(jQuery); $(function() {}); 窗口 反对。

    (函数($){})(jQuery); $(函数(){});

    参见下面的示例

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

    $(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");
    });
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Jordan Kasper    7 年前

    不幸的是,你不能用现有的代码来测试它们。这些函数是包装函数作用域的私有函数(不能在其外部访问)。但是,您可以重新组织代码以允许这种情况发生:

    // 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...
    });
    
    推荐文章