3

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

Haim Evgi
  • 123,187
  • 45
  • 217
  • 223
joshjdevl
  • 7,092
  • 12
  • 45
  • 57
  • 1
    mockjax seems nice. http://enterprisejquery.com/2010/07/mock-your-ajax-requests-with-mockjax-for-rapid-development/ will keep question open if someone has a suggestion that is integrated into jquery itself as opposed to a plugin – joshjdevl Feb 03 '11 at 07:22

3 Answers3

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.

Raynos
  • 166,823
  • 56
  • 351
  • 396
  • thank you for the ideas. Can you please share some links or further insights regarding mocking using node.js? – joshjdevl Feb 02 '11 at 04:06
  • node.js windows will need cygwin or mingw. will take a look at extending the jquery ajax instead – joshjdevl Feb 02 '11 at 04:44
  • @joshjdevl you can use php, perl or any webserver backend you want to mock out fake returned ajax request. If you got IIS set up you can even boot up an ASP.NET project. Read the `$.ajax` tests in jQuery to see what they do. – Raynos Feb 02 '11 at 09:14
0

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.

Community
  • 1
  • 1
Bruno Thomas
  • 1,179
  • 17
  • 31
0

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

Fred Yang
  • 2,521
  • 3
  • 21
  • 29