我有一个单元测试,其中唯一的期望是发出http呼叫.我为此使用$httpBackend.expect().这样工作正常,单元测试失败,如果这个http请求没有(这是好的),并通过,如果http请求.
问题在于,即使想到这一点,茉莉花规范竞赛者对这个单元测试显示“SPEC HAS NO EXPECTATIONS”,这使我觉得我没有使用推荐的方式来测试一个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();
}));