jeudi 26 mai 2016

mocking xhr calls in Jasmine

I have a question regarding mocking xhr in Jasmine. I have the following Javascript situation:

function Test1(){
 // some code
 Test2({ readyState: 4, responseText: "", status: 200, statusText: "OK" });
}
function Test2(xhr){
    var token = xhr.getResponseHeader("csrftoken");
    var csrfCtrl = $("#token");
    if (token != null && csrfCtrl != null) {
        csrfCtrl.val(token);
    }
}

Now I want to spyOn the xhr.getResponseHeader() function but I can not find out how I could do that.

I tried something like this:

describe("1 || Test ||", function () {    
        // Before we describe the tests we first need to setup a few things
        beforeEach(function () {
            // Load the first function of the HTML fixtures (htmlfixtures.js)
            setUpHTMLFixture1();
            jQuery.xhr = spyOn(jQuery.fn.ajax.xhr, "getResponseHeader").and.returnValue("null");             
        });
        it("1.1 # Check xhr functionality", function () {
          expect(jQuery.xhr).toHaveBeenCalled();    
        });
    });

But that did not work. Any ideas? Perhaps important to note. I use jQuery 1.8.

Aucun commentaire:

Enregistrer un commentaire