Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular JS Unit Testing $httpBackend spec has no expectations

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();
    });
});
like image 413
Travis Collins Avatar asked Nov 19 '14 11:11

Travis Collins


1 Answers

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();

}));
like image 74
Bradley Braithwaite Avatar answered Sep 19 '22 18:09

Bradley Braithwaite