How to properly unit test jQuery's .ajax() promises using Jasmine and/or Sinon?

前端 未结 4 1004
面向向阳花
面向向阳花 2020-12-07 10:59

I\'ve got a fairly straightforward function which returns a jQuery .ajax() promise as such:

CLAW.controls.validateLocation = function(val, $inputEl) {
    re         


        
4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 11:36

    The solution given by @ggozad won't work if you use things like .complete().

    But, hooray, jasmine made a plugin to do exactly this: http://jasmine.github.io/2.0/ajax.html

    beforeEach(function() {
      jasmine.Ajax.install();
    });
    
    afterEach(function() {
      jasmine.Ajax.uninstall();
    });
    
    //in your tests
    expect(jasmine.Ajax.requests.mostRecent().url).toBe('/some/cool/url');
    

提交回复
热议问题