10

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();
    });
});
7
  • Could add your code here just to illustrate it a bit? It could be some tiny mistake elsewhere Commented Nov 19, 2014 at 12:11
  • I tried to recreate the situation here: plnkr.co/edit/aGbBudhd3hYGa98g4V12?p=preview . But i did not get that warning, can you check for differences? Commented Nov 19, 2014 at 13:03
  • Looks like this is because I'm using Jasmine 2.1.1. Here's a new plunkr (I couldn't find a cdn for Jasmine 2.1.1) plnkr.co/edit/3AZotN?p=preview Commented Nov 19, 2014 at 13:52
  • 2
    I have the same problem. As a (really ugly) workaround I add expect(true).toBeTruthy() to every test just to remove this warning. Commented Dec 19, 2014 at 10:36
  • 4
    I'm using something a bit less ugly: expect('Suppress SPEC HAS NO EXPECTATIONS').toBeDefined(); Commented Mar 6, 2015 at 14:22

2 Answers 2

17

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

}));
Sign up to request clarification or add additional context in comments.

Comments

-1

Try to remove jasmine.clock().tick(30001);

It's excess in this code if you don't have timeout inside SessionTrackerService.Start method

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.