$ httpBackend.when(”)和$ httpBackend.expect(”)之间有什么区别?
我不知道这两种方法的区别。另外,angularjs api doc并没有帮助我。
API文档链接:https://docs.angularjs.org/api/ngMock/service/ $ httpBackend
$ httpBackend.expect – 指定请求期望
$ httpBackend.when – 指定后端定义
$ httpBackend.when – 指定后端定义
From: 07000$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.
因此,这意味着如果您设置请求期望,如果您没有获得完全相同的请求,确切次数,则测试将失败。但是,如果您将其设置为when,则后端将会适当地进行响应,但是对于有多少个请求(如果有的话)将不会有任何预期,因此不会失败。