【发布时间】:2018-12-29 23:57:14
【问题描述】:
我知道已经有很多帖子涵盖了这一点,但我在基线时并不擅长 Javascript,而且大多数可能应该有意义的帖子都使用了让我感到困惑的旧语法。
这是我想要工作的功能:
function getUpdateForm() {
$.ajax({
url: 'test_response.html',
type: 'GET',
dataType: 'json',
success: function(data) {
$("#Form_div").html(data);
},
});
};
这是我的回复文件:
<html>
<head>
<title>Test Response</title>
</head>
<body>
<h1>Test Page</h1>
</body>
</html>
这是我的 QUnit 测试:
QUnit.test('getUpdateForm ajax request', function(assert) {
$.ajax = function(request) {
assert.equal(
request.url,
'test_response.html',
'request url is correct'
);
assert.equal(request.type, 'GET',
'request type is correct'
);
assert.equal(request.dataType, 'json',
'request dataType is correct'
);
};
getUpdateForm();
setTimeout(function() {
assert.equal($("#Form_div").html(), '',// not exactly sure what to put
'Received correct html in response'
);
assert.async();
}, 1000);
});
目前它甚至没有尝试在setTimeout 函数中运行assert.equal。
请提供尽可能多的细节细节,我可能会有很多问题。第一个是,测试如何从$.ajax = function(request) 获得正确的功能?
【问题讨论】:
标签: javascript jquery unit-testing qunit