2014-01-15 46 views

回答

2

这里是我稍微hackie感觉解决方案,但它为我工作。

it('test beforeSend', function() { 

    spyOn(window, 'methodToBeTested') 

    spyOn($, "ajax").andCallFake(function(options) { 

     options.beforeSend(); 
     expect(methodToBeTested).toHaveBeenCalled(); 

     //this is needed if you have promise based callbacks e.g. .done(){} or .fail(){} 
     return new $.Deferred(); 
    }); 

    //call our mocked AJAX 
    request() 
}); 

你也可以试试Jasmine AJAX插件https://github.com/pivotal/jasmine-ajax

+0

我喜欢它,因为它很好,它的工作 –

0

您可以使用此:

if ($.isFunction($.fn.beforeSend)) { 
//function exists 
} 
+0

这不回答这个问题。 – undefined

+0

@BlackSheep:为什么? –

0

或者您也可以访问传递到暗中监视Ajax调用后的参数:

$.ajax.calls.argsFor(0)[0].beforeSend(); 
//instead of argsFor(), you can use first(), mostRecent(), all() ... 

这是如果你不希望有beforeSend()调用的每个时间。

相关问题