我正在做这个ajax调用:
$.ajax(this.apipath + "/login/refreshToken/", {
type: "POST",
async: false,
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Token ' + localStorage.getItem("loginToken"));
},
dataType: "json",
success: function (response) {
alert("success");
self.set('authenticated', true);
self.set('user', JSON.stringify(response.user));
self.set('loginToken', response.loginToken);
localStorage.setItem("loginToken", response.loginToken);
window.$.ajaxSetup({
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Token ' + response.loginToken);
}
});
}
});
现在我想用Jasmine写一个测试。我使用SinonJS来创建一个假的服务器。我想测试,如果调用了success函数并且是否设置了loginToken项。因此我使用这个指令:
server.respondWith(session.apipath + "/login/refreshToken/", [200, { 'Content-Type': 'application/json' }, JSON.stringify([{user:"asdf",loginToken:true}])]);
调用成功,但响应对象不包含loginToken属性。如何让服务器为成功函数提供正确的响应对象?