Differences between testing JavaScript and testing Ajax?
i was wondering if there is any difference between performing tests on JavaScript, or Ajax?
I understand that JavaScript is part of the Ajax equation, but is there any forms of execution differences when I am performing tests for JavaScript or JavaScript 开发者_高级运维which is used to enable AJAX?
Best Regards.
Ajax means your code performs requests to the server, usually using the XMLHttpRequest object. If your test of that code involves a server, you may end up actually testing more server code then JavaScript. If you are fine with that, you can use a testing framework like QUnit(part of the jQuery project, but doesn't require jQuery) or YUI Test that supports asynchronous tests, which are necessary here.
Otherwise you should replace the actual ajax request with a mock request that just returns some testdata, without ever performing an actual request. There are various solutions for that, but I need more context for any specific recommendation. Though, if you are using jQuery's ajax module, you can leverage the xhr-option for replacing the XMLHttpRequest with a mock object.
I think an important part of AJAX is the first A: Asynchronous.
In my (albeit small) experience with testing AJAX is that oftentimes you need to wait for something to happen: Wait for some text to appear on your html, wait for an image to disappear, wait for some style to be applied... I think this is one of the key differences between testing "regular" JavaScript code versus AJAX scripts.
精彩评论