单元测试 – 如何在Angular中覆盖$httpBackend的模拟响应?
发布时间:2020-12-17 08:04:17 所属栏目:安全 来源:网络整理
导读:是否可以在模拟的$ httpBackend中覆盖或重新定义模拟响应? 我有这样的测试: beforeEach(inject(function ($rootScope,$controller,_$httpBackend_) { $httpBackend = _$httpBackend_; //Fake Backend $httpBackend.when('GET','/myUrl').respond({}); //Em
是否可以在模拟的$ 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 })); 我怎么做?
文档似乎暗示了这种风格:
var 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'} }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |