mardi 19 mai 2015

How to get a Sinon.js fake server call the success function?

I am using Jasmine and Sinon to test an ajax call. The test looks like this:

describe("function initialize", function(){

    var server;

    beforeEach(function(){
        server = sinon.fakeServer.create();
    });

    afterEach(function() {
        server.restore();
    });

    it ("should make an ajax call successfully", function(){
        server.respondWith(session.apipath + "/login/refreshToken/",
            [200, { 'Content-Type': 'application/json' },
            '{user:"asdf",loginToken:"bla"}']);
        session.initialize();
        server.respond();
        expect(localStorage.getItem("loginToken")).toBe("bla");
    });
});

I want it to call the success function.

$.ajax(this.apipath + "/login/refreshToken/", {
    success: function (response) {
        alert("success!!!");
        localStorage.setItem("loginToken", response.loginToken);
    },
    error: function(){
        alert("error!!!");
    }
});

But only error is called. What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire