AJAX调用的Sinon超时
Sinon timeouts on AJAX call
使用Mocha Chai和Sinon,我有一个测试,从一个揭示模块模式中获得一个特定的记录。测试超时失败。我应该如何测试从AJAX请求分配变量的方法?
. js
(function () {
'use strict';
describe('Employee Module', function() {
var server,
employeeJSON = {
"employeeTemplate" : [
{
"userId": 1
}
]
};
before(function () {
server = sinon.fakeServer.create();
server.respondWith(
"GET",
"/employees.json",
[200, { "Content-Type": "application/json" }, JSON.stringify(employeeJSON)]
);
});
after(function () {
server.restore();
});
it('should get Employee by ID', function(done) {
var employee = new Employee(),
employeeData;
employee.getData(1).done( function (data) {
employeeData = data.employeeTemplate[0];
assert.equal(employeeData.userId, 1, 'Employee ID equals 1');
done();
});
});
});
})();
Employee.js
var Employee = function() {
var EmployeeInfo = {};
var loadUserinfo = function(userid) {
return $.ajax({
type: 'GET',
data:{userid: userid},
url: '/employees.json',
dataType: 'json',
async: true,
success: function(data) {
return data;
}
});
};
var getData = function (userid) {
return loadUserinfo(userid).done();
};
return {
getData: getData
};
};
您需要告诉FakeServer何时响应。参考FakeServer API文档
例如:it('should get Employee by ID', function(done) {
var employee = new Employee(),
employeeData;
employee.getData(1).done( function (data) {
employeeData = data.employeeTemplate[0];
assert.equal(employeeData.userId, 1, 'Employee ID equals 1');
done();
});
server.respond(); // Please respond mr. sinon so my test may continue
});
相关文章:
- 未调用 Sinon 存根
- 使用优化器在慢速连接上加载main.js时需要js超时
- 当浏览器控制台未打开时,为什么要求会导致Internet Explorer 9超时
- 超时功能的性能
- 使用不同的超时对数组中的每个项目进行角度动画处理
- 设置第一次执行的超时
- node.js测试事件是否是在不使用超时的情况下使用sinon.js发出的
- 使用javascript和随机暂停/超时快速循环文本
- 如何在jQuery事件处理程序中存储和重用超时
- 当定位模式设置为“0”时,总是达到关于地理定位的超时;仅设备”;
- 为什么页面重新加载会使设置超时无效,以及如何避免
- 立即设置超时
- 在promise中为回调添加超时并拒绝
- 如何设置$.ajax请求的超时,并在超时时间过长时进行重做
- Sinon Spy不使用Javascript调用或应用程序
- sinon.useFakeTimers不;t点火超时
- Sinon-带有回调的存根函数-导致测试方法超时
- 助焊剂 + Sinon + 承诺测试总是通过或超时
- Sinon 存根超时 Phantomjs
- AJAX调用的Sinon超时