在阅读了$httpBackend的AngularJS文档后,我还是不太清楚$httpBackend.expect
和$httpBackend.when
之间的区别,因为两者都是函数模拟 http 请求。
谁能帮忙解释一下这两个函数的区别?还有我什么时候应该使用 $httpBackend.when
而不是 $httpBackend.expect
?
最佳答案
答案很明确in the documentation ,但是,这是主要区别:
$httpBackend.when
将返回一个伪造的 http 响应,但如果声明的 URL 从未被调用,则不会通过测试。
$httpBackend.expect
会返回一个假的 http 响应。但如果从未调用声明的 URL,则测试失败。
因此,expect
设置了一个测试期望,即测试必须调用设置的 URL。
关于javascript - $httpBackend.expect 与 $httpBackend.when,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30799074/