茉莉模拟Ajax和JSON [英] jasmine mock-ajax with JSON

查看:210
本文介绍了茉莉模拟Ajax和JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图嘲笑/存根我ajax调用,但它看起来像它仅适用于文本。当我试图设置响应与对象,然后我做过回调不叫:

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
});

,但是当我设置的responseText与对象,然后它叫,但responseJSON没有设置,当FF调试响应

, 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?

我是用茉莉花2.1.3和最新版本的茉莉花阿贾克斯(老实说,我无法弄清楚我在哪里可以找到我使用的版本... ;-),我刚刚下载了模拟ajax.js从在github上的文档链接)

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)

更新: 这是我不好。我曾试图推动纯对象,使用时 JSON.stringify 并将结果传递到responseText的它的作品!

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

推荐答案

,你需要为使用的responseText或responseJSON,这取决于您的存根数据的类型。

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

如果你的回答是已经解析JSON(即实际JSON对象而不是一个字符串),然后使用responseJSON。

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

这篇关于茉莉模拟Ajax和JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆