Reputation: 83244
I am looking for a mature framework that can do Javascript mocking, especially on AJAX and JSON area.
Is there any existing mocking framework for Javascript ( and/ or JQuery) that you are used and can recommend?
Edit: I have evaluated jqmock and jqunit. It seems that it's the only framework that can do basic library mocking and stubbing, but it can't do AJAX call.
Upvotes: 13
Views: 7692
Reputation: 3093
Mocking is included in the jasmine testframework too. I used it myself to mock out jQuery.ajax:
spyOn(jQuery, 'ajax').andCallFake(function(value) {
value.success(data);
});
Upvotes: 2
Reputation: 54605
Here are some links that might help you:
amok (claims to have ajax support)
Other javascript testing "frameworks"
Upvotes: 16
Reputation: 54593
I prefer modular code. Modular code is easy to mock!
var function fetchAjaxData(){
// Perform ajax request.
// Call 'update' with the data in question.
}
var update = function(ajaxData){
// do things with ajaxData
}
With this kind of code, all you need to do is to call the 'update' function to mock a ajax request.
Upvotes: 4