【问题标题】:$httpBackend handling more than 3 calls$httpBackend 处理超过 3 个调用
【发布时间】:2014-02-17 18:56:51
【问题描述】:

我目前正在运行 angularjs 1.2.10 并使用 karma/jasmine 和 angular-mocks-1.2.10 进行单元测试,并卡在 $httpBackend 的单元测试用例中。

里面是块

describe('sumCtrl'...)
 ...
beforeEach(angular.mock.inject(function($rootScope,$controller,$httpBackend){
 scope = $rootScope.$new();
 httpBackend = $httpBackend;
 $controller('sumCtrl',{$scope:scope});
}));

it("should call these http services",function(){
 httpBackend.expectGET('/api/something1/').respond({success:true});
 httpBackend.expectPOST('/api/something2/').respond({success:true});
 httpBackend.flush();

});

上面的代码运行良好,但是当我再添加一个 httpBackend 调用时

    it("should call these http services",function(){
     httpBackend.expectGET('/api/something1/').respond({success:true});
     httpBackend.expectPOST('/api/something2/').respond({success:true});
     httpBackend.expectPOST('/api/something3/').respond({success:true});
     httpBackend.flush();

    });

这会在第 4 行出现错误。未满足的请求:POST '/api/something3' .... 使用 $httpBackend....

不知道在它块中使用 $httpBackend 发出的请求数量是否有限制,或者在使用 $httpBackend 时需要牢记的其他内容。

【问题讨论】:

标签: angularjs unit-testing jasmine karma-runner httpbackend


【解决方案1】:

我做了一个小提琴,如果你可以拥有超过 2 个:http://fiddle.jshell.net/gZS5M/

$http.get('/api/something1');
$http.post('/api/something2');
$http.post('/api/something3');
$http.post('/api/something4');

工作正常。我猜你只是在你的网址中有错字。

如果这没有帮助,请随时回击我

【讨论】:

  • 这对我有帮助。 expectPOST() 抛出错误,但是当我使用 when() 时,错误消失了。并且测试用例通过 -_-
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
  • 2011-05-15
  • 2020-10-27
  • 2012-03-06
  • 2021-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多