$httpBackend 不模拟 $.ajax(...) 调用

$httpBackend does not mock $.ajax(...) calls

正在尝试使用 $httpBackend.

为 $.ajax 调用创建模拟

下面的模拟代码片段与 $http.get('/phones')

一起工作得很好
$httpBackend.whenGET('/phones')

但是当尝试对

使用相同的内容时
$.ajax({
       url: '/phones',
       type: 'GET'
})

这为“/phones”ajax 调用引发了 404 错误。

Jsfiddler link here.

正如 $httpBackend documentation 的第一行所述,$httpBackend

Fake HTTP backend implementation suitable for unit testing applications that use the $http service.

如果您不使用 $http 服务,那么 $httpBackend 将不知道该请求,因此将无法拦截它或模拟响应。

您应该始终使用 $http - 没有理由在 angular 应用程序中使用 $.ajax