嘲讽$ httpBackend - 如何处理"意外的请求,没有更多的要求和期望;?QUOT [英] Mocking $httpBackend - how to handle "Unexpected request, No more request expected"?

查看:151
本文介绍了嘲讽$ httpBackend - 如何处理"意外的请求,没有更多的要求和期望;?QUOT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个茉莉花测试为codeD是这样的:

I have a Jasmine test that is coded like this:

  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);
});

和它种工作,但我得到的错误:

and it kind of works, but I get the error:

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)....

我不明白,我可以模拟所有其他呼叫。但是,让我们说,我不在乎我的测试要加载,因为它可以调用其他一些东西什么。
我怎样才能确保每个其他要求只是静静地发生,可能提供的一切单个虚拟回应别人?

I do realise that I can mock every other call. But let's say I do not care what else my test wants to load as it may call few other things. How I can make sure that every other requests just "happen silently", maybe offering a single dummy response for everything else?

推荐答案

由于提出要求,你没有指定你的测试失败。

Your test fails because a request is made which you haven't specified.

尝试添加:

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

当然,你也应该定义 fakedMainResponse

请看看也是在文档(部分请求期望VS后端定义),它说:

Please take a look also at the documentation (section Request Expectations vs Backend Definitions) which says:

请求的预期提供了一种方法,使有关请求的断言
  由应用程序提出并定义这些请求的响应。
  测试将如预期的请求不作出或失败他们
  以错误的顺序进行。

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 第二paramete实际上是一个正则表达式。所以,如果你提供了一个正则表达式将匹配所有其他请求它应该工作。

The second paramete of $httpBackend.when is actually a RegExp. So if you provide a RegExp that will match all other requests it should work.

这篇关于嘲讽$ httpBackend - 如何处理"意外的请求,没有更多的要求和期望;?QUOT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆