如何在Quit中模拟.load函数
How to mock-up the .load function in Qunit?
我有一个javascript函数要测试,它包含.load
function getPane(divId) {
$("#" + divId).load(
"Pane.html",
function () {
//do some work here
});
});
}
我想用Quit测试一下,但我不知道如何嘲笑这种行为。
我也不知道如何模拟一个同时具有.load和.get-的函数
function getPane(divId) {
$("#" + divId).load("Pane.html", function () {
$.get("/Config/Pane", function (data) {
//do work here
}
});
});
}
我只使用QUnit,没有Mockjax或Sinon.js或任何东西(我知道,我知道我应该这样做)。如有任何帮助,我们将不胜感激。谢谢
由于OP建议他们可以使用Mockjax,我想我应该添加那个解决方案。请注意,我在一个设置方法中添加了mock,然后将其拆下。这允许每个测试都是幂等的。此外,getPane()
函数需要回调,以便在测试中添加断言。
function getPane(divId, cb) {
$("#" + divId).load("Pane.html", function () {
$.get("/Config/Pane", function (data) {
// do work here
cb(); // callback executed for any additional actions (like tests)
// you may want to add some error handling with callback as well
});
});
}
然后在qunit测试文件的#qunit-fixture
中添加div以将内容放入:
<html>
...
<body>
<div id="qunit"></div>
<div id="qunit-fixture">
<div id="foobar"></div> <!-- our test element -->
</div>
...
</body>
</html>
现在写你的模拟和测试:
QUnit.module("some tests", {
setup: function() {
$.mockjax({
url: "Pane.html",
responseText: "<div>Some HTML content</div>"
});
},
teardown: function() {
$.mockjax.clear(); // new in 1.6
}
});
QUnit.asyncTest("test it out", function(assert) {
getPane("foobar", function() {
assert.equal($("#foobar div").length, 0, "A new div was added to the page!");
QUnit.start();
});
});
相关文章:
- 在VanillaJS中模拟模型双向数据绑定
- 正在寻找比$(document).ready慢的$(window).load的替代方案
- 使用jasmine模拟对服务器的调用
- 模拟谷歌地图中的点击
- 使用模拟按键在输入框中自动输入文本
- 模拟chrome.storage.local函数使用Jasmine
- 从数据库中检索字段,而不模拟它们
- jQuery点击ON现在使用.load触发关闭
- react testUtils模拟点击单选按钮而不触发onchange
- .load()函数赢得't加载javascript
- 使用jquery.load('pageName')方法时,未从应用程序缓存加载Html页
- 使用.load()后在Lightbox中插入谷歌地图
- 如何在ember单元测试中模拟_super()方法
- Javascript中对象的每个()的模拟值
- 模拟<按钮>在<asp:button>
- .load ajax无法正常工作whitin wordpress主题
- javascript上的数字类型模拟
- 在Debian中运行包含load()和print()函数的JS脚本
- 在Android Kitkat上模拟鼠标点击Youtube视频
- 如何在Quit中模拟.load函数