What is the difference between expect and when in $httpBackend

☆樱花仙子☆ 提交于 2019-11-30 13:44:19

问题


What is the difference between $httpBackend.when('') and $httpBackend.expect('')?

I don't know the difference between these two methods. Also the angularjs api doc does not help me.

API documentation link: https://docs.angularjs.org/api/ngMock/service/$httpBackend


回答1:


$httpBackend.expect - specifies a request expectation
$httpBackend.when - specifies a backend definition

From: https://docs.angularjs.org/api/ngMock/service/$httpBackend
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.

Backend definitions allow you to define a fake backend for your application which doesn't assert if a particular request was made or not, it just returns a trained response if a request is made. The test will pass whether or not the request gets made during testing.

Therefore, it means that if you set a request expectation with expect the test will fail if you don't get the exact same request, exact number of times. However if you set it with when, the backend will respond appropriately, but it has no expectations about how many requests (if any) will come therefore will not fail the test.



来源:https://stackoverflow.com/questions/27945758/what-is-the-difference-between-expect-and-when-in-httpbackend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!