微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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将匹配所有其他请求它应该工作。

原文地址:https://www.jb51.cc/angularjs/145615.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐