Ajax not executed with Jasmine
I have troubles executing ajax with the Jasmine BDD framework.
I want to test the actual ajax calls, not make fake responses. I have read the documentation and tried everything but it seems that the ajax code is simply ignored. i have also tried using spies but it doesn't seem to help.
A very basic example that is not working:
describe("A jQuery ajax test", function() {
it("should make AJAX request", function 开发者_Go百科() {
expect(testAjax()).toBe(1);
});
});
function testAjax() {
var ret=0
$.ajax({
type: "GET",
url: "obj.json",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){ret=1;}
});
return ret;
}
The return is always 0, it never enters the success function.
What am I doing wrong?
Anwering my own question. Ajax calls in Jasmine needs to be async. If you do not want to change your code to be able to test it you can use the ajaxSetup to set the dafault value for async to be false
it("getsetting", function () {
$.ajaxSetup({
async:false
});
expect(getUserSetting(101,0)).toBe('30');
});
While setting async to false solves the problem it's usually not an option in most of web applications because whole page will be locked during the ajax call.
I would pass in callback function to testAjax() method that would be executed when you get response from the web service. You can then make an assertion(expect) inside your callback.
It should look something like this:
function testAjax(callback) {
var ret=0
$.ajax({
type: "GET",
url: "obj.json",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){
callback(ret);
},
error:function(){
callback(ret);
}
});
}
describe("A jQuery ajax test", function() {
it("should make AJAX request", function () {
testAjax(function(ret){
expect(ret).toBe(1);
});
});
});
I understand that you would like to write an Integration test using jasmine, which is quite possible thanks to Async Support that jasmine has, I hope following example would help you understand how you can useAsync features of jasmine to write the an actual integration test:
describe("A jQuery ajax test", function() {
it("should make AJAX request", function () {
var return = null;
runs(function(){
// hosts actual ajax call
return = testAjax();
});
waitsFor(function(){
//this method polls until the condition is true or timeout occurs,whichever occurs first
return return.readyState==4;
},"failure message",700//timeout in ms
);
runs(function(){
// 2nd runs block can be used to verify the actual change in state
// Add other relevant expectation according to your application context.
expect(return).toBeTruthy();
});
});
});
function testAjax() {
// result here is a deffered object whose state can be verified in the test
var result = null;
result = $.ajax({
type: "GET",
url: "obj.json",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function() { //do something on success}
});
return ret;
}
Please note: While running the AJAX call you will be restricted by Cross-origin_resource_sharing and your server must return as per part of the response a header "Access-Control-Allow-Origin:your requesting domain "
精彩评论