jquery 1.5 mock ajax

徘徊边缘 提交于 2019-12-06 09:55:08

问题


In jquery 1.5, what is the recommend approach for mocking ajax requests? Is it to use the ajax extension transports? http://api.jquery.com/extending-ajax/#Transports


回答1:


Have you tried extending $.ajax with the 1.5 update and finding out if you can call .resolve on the jqXHR.

As long as you get it to not remove the .promise read-only Deferred object you should be able to fake a completion of the ajax request.

Alternative you can always just mock out a node.js server.

Anywhere you can hook into and get the jqXHR object you might be able to call .resolve on it with the mock paramaters.




回答2:


I have a similar question here :

How to test jquery and ajax calls using JsTestDriver?

And I found solutions, maybe there are others better, but it works with our JsTestDriver tests.




回答3:


I have created on mock for jQuery AJAX, and I used three extension points that jQuery 1.5+ provided (prefitler, transport, and converters). Please check this out

https://github.com/fredyang/ajaxMock

for detail usage, please check the test case, all the test cases are passed, and they are just the living document about how to use it.

Here is the url of the test case

https://github.com/fredyang/ajaxMock/blob/master/test/test.js

Here is the link that run the test case

http://code.semanticsworks.com/ajaxMock/test/index.html

and here is the link of the mock

http://code.semanticsworks.com/ajaxMock/src/ajaxMock.js



来源:https://stackoverflow.com/questions/4869926/jquery-1-5-mock-ajax

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