Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用qunit测试代码时出错_Javascript_Jquery_Qunit - Fatal编程技术网

Javascript 使用qunit测试代码时出错

Javascript 使用qunit测试代码时出错,javascript,jquery,qunit,Javascript,Jquery,Qunit,这是应用程序的代码。 所有rest函数都是从init()调用的 如何开始使用qunit测试代码,因为如果我直接调用tests.js文件中的函数,它会显示“ReferenceError:init未定义”” 您的问题是,您正在一个内声明您的init函数,而该函数范围之外的任何对象都无法访问该函数。您可以通过使用一个非常简单的“模块”模式来解决这个问题,该模式从IFFE返回init方法并将其分配给变量 JS // "namespace" for your application. // Whateve

这是应用程序的代码。 所有rest函数都是从
init()
调用的

如何开始使用qunit测试代码,因为如果我直接调用tests.js文件中的函数,它会显示“
ReferenceError:init未定义”


您的问题是,您正在一个内声明您的
init
函数,而该函数范围之外的任何对象都无法访问该函数。您可以通过使用一个非常简单的“模块”模式来解决这个问题,该模式从IFFE返回init方法并将其分配给变量

JS

// "namespace" for your application.
// Whatever your IFFE returns is assigned to the App variable
// this allows other scripts to use your application code
var App = (function ($, undefined) {

    // some code here 
    // and has variables and functions defined which get called inside this and are all interdependent.

    // example of a function inside your "application" js
    var printTitle = function () {
        var title = document.title;
        console.log(title);
    }

    var init = function () {
        printTitle();
    }

    // expose internal methods by returning them.
    // you should probably be exposing more than your init method
    // so you can unit test your code
    return {
        init: init
    }
})(jQuery);


// since we've returned the init function from within our iffe
// and that function is assigned to the App variable
// we are able to call App.init here
App.init(); // logs title

我发现以下文章有助于进行js测试:


您是如何运行测试的?通常,qUnit从加载以下内容的html文件运行:1。昆特2。应用程序的js文件3。您的测试js文件我正在从qunit.html文件运行测试。但问题是我不知道如何为现有的.js文件编写测试用例,需要一些指导。你的问题是你在一个函数中声明了你的init函数,而这个函数范围之外的任何东西都无法访问它。您需要以某种方式将其公开到全局范围。如何将其公开到全局范围?请您解释一下代码以及“名称空间”。我甚至尝试过使用您的代码。现在它表示未捕获的TypeError:无法调用undefinedTypeError的方法“init”:无法读取undefined的属性“init”。这个错误意味着什么?
// "namespace" for your application.
// Whatever your IFFE returns is assigned to the App variable
// this allows other scripts to use your application code
var App = (function ($, undefined) {

    // some code here 
    // and has variables and functions defined which get called inside this and are all interdependent.

    // example of a function inside your "application" js
    var printTitle = function () {
        var title = document.title;
        console.log(title);
    }

    var init = function () {
        printTitle();
    }

    // expose internal methods by returning them.
    // you should probably be exposing more than your init method
    // so you can unit test your code
    return {
        init: init
    }
})(jQuery);


// since we've returned the init function from within our iffe
// and that function is assigned to the App variable
// we are able to call App.init here
App.init(); // logs title