Javascript 如何正确使用Jasmine的模拟ajax库
我有一个调用JSON的函数,然后在success函数中对DOM进行一些更改。我试图在Jasmine测试中使用,以避免暴露各种私有函数进行模拟 即使在单步执行测试时设置了Javascript 如何正确使用Jasmine的模拟ajax库,javascript,jquery,jasmine,jasmine-ajax,Javascript,Jquery,Jasmine,Jasmine Ajax,我有一个调用JSON的函数,然后在success函数中对DOM进行一些更改。我试图在Jasmine测试中使用,以避免暴露各种私有函数进行模拟 即使在单步执行测试时设置了request.response,也不会调用onSuccess方法 我的测试: describe('the table loader', function () { var request; beforeEach(function() { //html with a lo
request.response
,也不会调用onSuccess方法
我的测试:
describe('the table loader', function () {
var request;
beforeEach(function() {
//html with a loader div, an output div and a transit and dwell template
setFixtures('<div class="loader"></div><div class="row"><div id="output" class="col-xs-12"></div></div><script id="thingTemplate" type="text/x-handlebars">{{snip}}</script>');
expect($('.loader')).toBeVisible();
//start ajax call
window.dashboards.thing.display({
loaderId: '.loader',
templateId: '#thingTemplate',
templateOutletId: '#output',
dataUrl: '/my/fake/url'
});
//catch the ajax request
request = mostRecentAjaxRequest();
});
describe('on success', function () {
beforeEach(function() {
//populate the response
request.response({
status: 200,
responseText: "{rowItem: [{},{},{}]}"
});
});
it('should hide the loader', function () {
//thing should now receive that JSON and act accordingly
expect($('.loader')).not.toBeVisible();
});
});
});
没有调用任何延迟函数(success、error和always)
编辑
根据@gregg下面的回答(他是对的,我没有在示例代码中包含UseMock调用),这感觉像是版本问题。即使包括了那个电话,这对我来说仍然不起作用
我添加了一个,您需要确保在实际进行调用之前使用
jasmine.ajax.useMock()
安装ajax模拟,否则jasmine ajax不会接管XHR对象,您将发出真正的请求。一旦我针对您的示例代码这样做了,您发送的responseText
看起来不是JSON可解析的,jQuery就会崩溃。但我仍然在控制台中看到“complete”消息。因此,截至2014年1月29日,Github上模拟ajax自述文件中1.3.1标签中的下载链接没有指向文件的正确版本
从标记的lib文件夹手动下载模拟ajax文件是可行的
换句话说,对于1.3.1标记版,不要从下载下载下载,ajax调用和jasmine调用返回的数据格式可能不同——我相信这会引发无法恢复的异常,因此没有回调函数运行 较新版本的jasmine ajax也使用了
request.respondbwith
,而且值得注意的是(尽管我认为jQuery可以处理这个问题),jasmine没有定义XMLHttpRequest.Done
,所以如果您使用vanilla JS,您必须自己处理这个问题。最后,我使用jasmine.Ajax.install()
,而不是jasmine.Ajax.useMock()
呼——很难知道到底该怎么办,因为Jasmine的旧版本文档太差,而且它们之间有太多不一致的地方。AH!很高兴知道我不是完全疯了。是的,在编写示例代码时,我意外地去掉了useMock调用。但它仍然不适合我。我为GitHub添加了一个示例。您能看看我们使用的库的版本是否有差异吗?谢谢保罗
(function (dashboards, $) {
dashboards.thing = dashboards.thing || {};
var compileTable = function(templateId, jsonContext) {
var source = $(templateId).html();
var template = Handlebars.compile(source);
var context = jsonContext;
return template(context);
};
var getDashboardData = function(options) {
$.getJSON(
options.dataUrl,
function (data) {
processDashboardData(options, data);
}
).fail(function (jqxhr, textStatus, error) {
console.log('error downloading dashboard data');
console.log(textStatus + ': ' + error);
}).always(function() {
console.log('complete');
});
};
var processDashboardData = function (options, data) {
$(options.loaderId).hide();
$(options.templateOutletId).html(compileTable(options.templateId, data));
};
dashboards.thing.display = function (options) {
getDashboardData(options);
};
}(
window.dashboards = window.dashboards || {},
jQuery
));