mardi 5 mai 2015

Ajax Jasmine Karma

I am trying to create a test for ajax call. So below you can find test form jasmine documentation page.But I am not sure how my js should look like.Cuz it looks like it testing it without ajax cal from my js file.

AjaxSpec.js

describe("mocking ajax", function() {
      describe("suite wide usage", function() {
        beforeEach(function() {
          jasmine.Ajax.install();
        });
        afterEach(function() {
          jasmine.Ajax.uninstall();
        });

        it("specifying response when you need it", function() {
          var doneFn = jasmine.createSpy("success");
          var xhr = new XMLHttpRequest();
          xhr.onreadystatechange = function(args) {
            if (this.readyState == this.DONE) {
              doneFn(this.responseText);
            }
          };

          xhr.open("GET", "http://ift.tt/17vsUzp");
          xhr.send();

          expect(jasmine.Ajax.requests.mostRecent().url).toBe('http://ift.tt/17vsUzp');
          expect(doneFn).not.toHaveBeenCalled();

          jasmine.Ajax.requests.mostRecent().respondWith({
            "status": 200,
            "contentType": 'text/plain',
            "responseText": 'awesome response',
          });
          expect(doneFn).toHaveBeenCalled();
          expect(doneFn).toHaveBeenCalledWith('awesome response');
        });
      });
    });

ajax.js

sendOrder = function() {
  return $.ajax({
    type: "GET",
    url: "http://ift.tt/1E4qmT6",
    success: function(){
      return 'awesome response'
    },
    responseText:"awesome response"
  });
}

Aucun commentaire:

Enregistrer un commentaire