[英]Angular JS Unit Testing $httpBackend spec has no expectations
我有一个单元测试,其中唯一的期望是进行了http调用。 我为此使用$ httpBackend.expect()。 这样可以正常工作,如果没有发出这个http请求,单元测试会失败(这很好),如果发出了http请求则通过。
问题是,即使认为它通过,Jasmine spec跑步者显示“SPEC没有预期”这个单元测试,这让我觉得我没有使用推荐的方式来测试http调用。 如何避免看到此消息?
示例测试:
it('should call sessioncheck api', function () {
inject(function ($injector, SessionTrackerService) {
$httpBackend = $injector.get('$httpBackend');
var mockResponse = { IsAuthenticated: true, secondsRemaining: 100 };
$httpBackend.expect('GET', 'API/authentication/sessioncheck')
.respond(200, mockResponse);
SessionTrackerService.Start();
jasmine.clock().tick(30001);
$httpBackend.flush();
});
});
我将调用包装如下:
expect($httpBackend.flush).not.toThrow();
我更喜欢这种方法,因为测试代码清楚地说明了调用flush时应该发生什么。 例如:
it('should call expected url', inject(function($http) {
// arrange
$httpBackend.expectGET('http://localhost/1').respond(200);
// act
$http.get('http://localhost/1');
// assert
expect($httpBackend.flush).not.toThrow();
}));
尝试删除jasmine.clock()。tick(30001);
如果在SessionTrackerService.Start方法中没有超时,则此代码中的内容过多
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.