2014-12-02 13 views
11

Avere problemi a ottenere un semplice esempio di lavoro. Sto usando questo esempio tratto da https://gist.github.com/Madhuka/7854709

describe("Test for spies", function() { 
function sendRequest(callbacks, configuration) { 
     $.ajax({ 
      url: configuration.url, 
      dataType: "json", 
      success: function(data) { 
       callbacks.checkForInformation(data); 
      }, 
      error: function(data) { 
       callbacks.displayErrorMessage(); 
      }, 
      timeout: configuration.remainingCallTime 
     }); 
    } 

    it("should make an Ajax request to the correct URL", function() { 

    var configuration = { 
     url : "http://www.google.com", 
     remainingCallTime : 30000 
    }; 

     spyOn($, "ajax"); 

     sendRequest(undefined, configuration); 
     expect($.ajax.mostRecentCall.args[0]["url"]).toEqual(configuration.url); 
    }); 
}); 

Per qualsiasi motivo, $.ajax.mostRecentCall è indefinito.

Utilizzo di jasmine 2.0.2 e jasmine jquery 2.0.5.

Fiddle qui: http://jsfiddle.net/sidouglas/85b35993/

risposta

18

Questa vecchia 1.x Jasmine sintassi:

$.ajax.mostRecentCall.args 

La sintassi per Jasmine 2 è:

$.ajax.calls.mostRecent().args 

Quindi la tua asserzione dovrebbe essere:

expect($.ajax.calls.mostRecent().args[0]["url"]).toEqual(configuration.url); 
+1

Grazie per Prendendo il tempo di rispondere a questa domanda. – Simon

Problemi correlati