How to test XMLHttpRequest with Jasmine

后端 未结 6 1117
心在旅途
心在旅途 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条回答
  •  梦毁少年i
    2021-02-19 17:14

    You can test it in such manner

    it("should make XHR request", function() {
    
       // arrange
    
        var xhr = {
            open: jasmine.createSpy('open')
        };
    
        XMLHttpRequest = jasmine.createSpy('XMLHttpRequest');
        XMLHttpRequest.and.callFake(function () {
            return xhr;
        });
    
        // act
    
        submit();
    
        // assert
    
        expect(xhr.open).toHaveBeenCalled(); 
    });
    

提交回复
热议问题