Javascript 在Document.ready上调用函数的测试用例

Javascript 在Document.ready上调用函数的测试用例,javascript,jquery,qunit,blanket.js,Javascript,Jquery,Qunit,Blanket.js,我的JS代码如下所述: if (typeof IPL === "undefined") { IPL = {}; } /// <summary>Declare Namespace IPL.Register</summary> if (typeof IPL.Register === "undefined") { IPL.Register = {}; } $(document).ready(function () { IPL.Register.Pri

我的JS代码如下所述:

if (typeof IPL === "undefined") {
    IPL = {};
}

/// <summary>Declare Namespace IPL.Register</summary>
if (typeof IPL.Register === "undefined") {
    IPL.Register = {};
}

$(document).ready(function () {
    IPL.Register.Print.initialize();
});

/// <summary>Declare Namespace IPL.Register.Print</summary>
IPL.Register.Print =
{
    /// <summary>Function to call on initialize page.</summary>
    initialize: function () {
        window.onload = function () {
            window.print();
        };
    }
};

请有人帮助编写测试用例以在文档加载时执行函数?

最好的方法是根本不测试加载事件。只需将事件处理程序放入一个命名函数中,然后测试该函数的行为

/// <summary>Declare Namespace IPL.Register.Print</summary>
IPL.Register.Print = {
  /// <summary>Function to call on initialize page.</summary>
  initialize: function() {
    window.onload = function() {
      window.print();
    };
  },
  print: function() {
    window.print();
  }
};

test("initialize test", 1, function() {
  var result = IPL.Register.Print.print();
  equal(undefined, result, "passed");
});
///声明命名空间IPL.Register.Print
IPL.Register.Print={
///函数在初始化页面上调用。
初始化:函数(){
window.onload=函数(){
window.print();
};
},
打印:函数(){
window.print();
}
};
测试(“初始化测试”,1,函数(){
var result=IPL.Register.Print.Print();
相等(未定义,结果“通过”);
});


加载事件只是运行您的函数,您希望通过测试加载事件具体实现什么?

最好的方法是根本不测试加载事件。只需将事件处理程序放入一个命名函数中,然后测试该函数的行为

/// <summary>Declare Namespace IPL.Register.Print</summary>
IPL.Register.Print = {
  /// <summary>Function to call on initialize page.</summary>
  initialize: function() {
    window.onload = function() {
      window.print();
    };
  },
  print: function() {
    window.print();
  }
};

test("initialize test", 1, function() {
  var result = IPL.Register.Print.print();
  equal(undefined, result, "passed");
});
///声明命名空间IPL.Register.Print
IPL.Register.Print={
///函数在初始化页面上调用。
初始化:函数(){
window.onload=函数(){
window.print();
};
},
打印:函数(){
window.print();
}
};
测试(“初始化测试”,1,函数(){
var result=IPL.Register.Print.Print();
相等(未定义,结果“通过”);
});


负载事件只是运行您的函数,您希望通过测试负载事件具体实现什么?

您希望进行哪种测试?单元测试不应该依赖于document.ready事件。如果你想要一个e2e测试,你可以尝试一些类似我只是想提高代码覆盖率的东西。你想要什么样的测试?单元测试不应该依赖于document.ready事件。如果您想要e2e测试,您可以尝试类似于“我只想提高代码覆盖率”的方法。