我有一个加载模板的函数,我想检查是否调用了正确的 URL。
因为除了监视 ajax 调用之外我找不到任何信息,我假设调用是一样的.load()
。我正在使用茉莉花 2.4.1
功能
function templateLoader() {
var templateURL = '/path/to/template.html';
$('#myElement').load(templateURL, function(response, status, xhr) {
if (status === "error") {
common.templateError(templateURL, xhr);
} else {
ns.successFunction();
}
});
}
茉莉花测试
var templateURL = '/path/to/template.html';
spyOn($('#myElement'), "load");
templateLoader(); // call the function
expect($('#myElement').load.calls.mostRecent().args[0]["url"]).toEqual(templateURL);
当我运行此测试时,我收到以下错误:
TypeError:无法读取未定义的属性“mostRecent”
有不同的方法可以做到这一点吗?我还想检查是否调用了成功函数,但直到可以检查 URL 是否正确我不能这样做。