I have a unit test in which the only expectation is that a http call was made. I use $httpBackend.expect() for this. This works fine, and the unit test fails if this http request is not made (which is good), and passes if the http request is made.
The problem is that even thought it passes, Jasmine spec runner shows "SPEC HAS NO EXPECTATIONS" for this unit test, which makes me think I am not using the recommended way to test that a http call was made. How do I avoid seeing this message?
Example test:
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();
});
});
I wrap the call to flush as follows:
expect($httpBackend.flush).not.toThrow();
I prefer this approach because the test code clearly states what 'should' happen when flush is called. For example:
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();
}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With