是否可以在模拟的$ 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 }));
我怎么做?
我的代码现在是不可测试的:(
文档似乎暗示了这种风格:
原文链接:https://www.f2er.com/angularjs/144026.htmlvar myGet; beforeEach(inject(function ($rootScope,_$httpBackend_) { $httpBackend = $_httpBackend_; myGet = $httpBackend.whenGET('/myUrl'); myGet.respond({}); });
…
it('should work',function() { myGet.respond({foo: 'bar'}); $httpBackend.flush(); //now your response to '/myUrl' is {foo: 'bar'} });