How to test XMLHttpRequest with Jasmine

后端 未结 6 1156
心在旅途
心在旅途 2021-02-19 16:43

How can I test the onreadystatechange on XMLHttpRequest or pure Javascript AJAX without jQuery? I\'m doing this because I\'m developing Firefox extension. I guess I have to use

6条回答
  •  野性不改
    2021-02-19 17:13

    And what about this one?

    beforeEach(function() {
      // spyOn(XMLHttpRequest.prototype, 'open').andCallThrough(); // Jasmine 1.x
      spyOn(XMLHttpRequest.prototype, 'open').and.callThrough(); // Jasmine 2.x
      spyOn(XMLHttpRequest.prototype, 'send');
    });
    
    ...
    
    it("should call proper YQL! API", function() {
      podcast.load_feed('http://www.faif.us/feeds/cast-ogg/');
    
      expect(XMLHttpRequest.prototype.open).toHaveBeenCalled();
    });
    

    Pure Jasmine without need to use any external library.

提交回复
热议问题