加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – Mocking $httpBackend – 如何处理“意外请求,没有

发布时间:2020-12-17 08:37:12 所属栏目:安全 来源:网络整理
导读:我有一个茉莉花测试,编码如下: 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(fakedDto
我有一个茉莉花测试,编码如下:
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将匹配所有其他请求它应该工作。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读