How do we clear spy programmatically in Jasmine?

后端 未结 10 2426
孤独总比滥情好
孤独总比滥情好 2020-12-15 02:11

How do we clear the spy in a jasmine test suite programmatically? Thanks.

beforeEach(function() {
  spyOn($, \"ajax\").andCallFake(function(params){
  })
})
         


        
10条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 03:09

    This worked for me in Jasmine 2.5 to allow re-setting of mock ajax.

    function spyOnAjax(mockResult) {
        // must set to true to allow multiple calls to spyOn:
        jasmine.getEnv().allowRespy(true);
    
        spyOn($, 'ajax').and.callFake(function () {
            var deferred = $.Deferred();
            deferred.resolve(mockResult);
            return deferred.promise();
        });
    }
    

    Then you can call it multiple times without error. spyOnAjax(mock1); spyOnAjax(mock2);

提交回复
热议问题