是否可以在模拟的 $httpBackend 中覆盖或重新定义模拟响应?
我有这样的测试:
beforeEach(inject(function ($rootScope, $controller, _$httpBackend_) {
$httpBackend = _$httpBackend_;
//Fake Backend
$httpBackend.when('GET', '/myUrl').respond({}); //Empty data from server
...some more fake url responses...
}
这在大多数情况下都很好,但我很少有测试需要为同一个 URL 返回不同的东西。但似乎一旦定义了 when().respond() 之后,我就无法在这样的代码中更改它:
单个特定测试中的不同响应:
it('should work', inject(function($controller){
$httpBackend.when('GET', '/myUrl').respond({'some':'very different value with long text'})
//Create controller
//Call the url
//expect that {'some':'very different value with long text'} is returned
//but instead I get the response defined in beforeEach
}));
我怎么做?我的代码现在无法测试:(