angularjs – Mocking $httpBackend – 如何处理“意外请求,没有更多的请求预期”?

我有一个茉莉花测试,编码如下:
it ("should send correct message to server to get data,and correctly set up scope when receiving it",function(){
    $httpBackend.when('GET','https://localhost:44300/api/projectconfiguration/12').respond(fakedDtoBase);
    $routeParams.projectId=fakeId; // user asks for editing project
    scope.$apply(function(){
        var controller=controllerTotest(); // so controller gets data when it is created
    });
    expect(scope.projectData).toEqual(fakedDtoBase);
});

它的工作,但我得到的错误

Error: Unexpected request: GET views/core/main/main.html
No more request expected
    at $httpBackend (C:/SVN/src/ClientApp/client/bower_components/angular-mocks/angular-mocks.js:1207:9)
    at sendReq (C:/SVN/src/ClientApp/client/bower_components/angular/angular.js:7800:9)
    at $http.serverRequest (C:/SVN/src/ClientApp/client/bower_components/angular/angular.js:7534:16)
    (more stack trace)....

我意识到,我可以嘲笑每一个其他的电话。但让我们说,我不在乎我的测试想要加载什么,因为它可能调用少数其他的东西。
我如何确保每一个其他请求只是“静”,也许提供一个单一的虚拟响应其他一切?

您的测试失败,因为您未指定请求。

尝试添加

$httpBackend.when('GET','views/core/main/main.html').respond(fakedMainResponse);

当然你也应该定义fakedMainResponse。

请查看documentation(请求期望vs后端定义)部分,其中说:

Request expectations provide a way to make assertions about requests
made by the application and to define responses for those requests.
The test will fail if the expected requests are not made or they are
made in the wrong order.

$ httpBackend.when的第二个参数实际上是一个RegExp。所以如果你提供一个RegExp将匹配所有其他请求它应该工作。

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...