jasmine mock-ajax with JSON

落花浮王杯 提交于 2019-12-12 12:00:18

问题


I have tried to mock/stub my ajax call, but it looks like it only works with text. When I tried to set response with object then my done callback is not called:

jasmine.Ajax.stubRequest('/some_url/1').andReturn({
  //"responseText": response ,
 "response": response,
  "status": 200
});

, but when I set responseText with object then it is called, but the responseJSON is not set, when debug response in FF

responseJSON undefined
status 200
statusText "success"

Am I doing something wrong or it is an issue?

I am using Jasmine 2.1.3 and latest version of jasmine-ajax (honestly I cannot figure it out where I can find version I am using...;-), I've just downloaded mock-ajax.js from link in documentation in github)

UPDATE: It was my bad. I have tried to push plain Object, when used JSON.stringify and pass the result to "responseText" it works!


回答1:


Instead of using the "response" attribute, you need to use either "responseText" or "responseJSON", depending upon your stubbed data's type.

If your response is already parsed JSON (i.e., an actual JSON object and not a string), then use "responseJSON".



来源:https://stackoverflow.com/questions/28428748/jasmine-mock-ajax-with-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!