2

I have recently started writting unit tests using Karma + Karma-jasmine but I am having problems with the following tests:

describe("WEBSERVICE:", function () {

    var webservice,
        $httpBackend,
        authRequestHandler,
        webserviceURL = "http://localhost:8006/";


    beforeEach(inject(function (Webservice, $injector) {
        webservice = Webservice;
        $httpBackend = $injector.get("$httpBackend");


        authRequestHandler = $httpBackend
            .when("GET", webserviceURL + "users/login")
            .respond(200, "ok");
    }));


    afterEach(function() {
        $httpBackend.verifyNoOutstandingExpectation();
        $httpBackend.verifyNoOutstandingRequest();
    });


    it("should EXISTS", function () {
        expect(webservice).toBeDefined();
    });


    it("should throw a WebserviceError if we are not logged in" , function () {
        expect(function () {
            webservice.item("negs", "RPT");
        }).toThrow(webserviceAuthenticationError);
    });


    it("should NOT HAVE credentials when instantiated", function () {
        expect(webservice.hasCredentials()).toBeFalsy();
    });


    it("should log in when valid credentials are given", function () {
        $httpBackend.expectGET("users/login");
        webservice.withCredentials("sam", "password");
    });
});

It appears to be the following which creates the problem since all tests pass when I remove it:

afterEach(function() {
    $httpBackend.verifyNoOutstandingExpectation();
    $httpBackend.verifyNoOutstandingRequest();
});

I was just wondering if anyone could help me with this. Thanks a lot.

Nicholas Murray
  • 13,305
  • 14
  • 65
  • 84
blemaire
  • 33
  • 1
  • 3

1 Answers1

0

The reason you having problems is with

$httpBackend.verifyNoOutstandingExpectation();

is due to your last test

it("should log in when valid credentials are given", function () {
    $httpBackend.expectGET("users/login");
    webservice.withCredentials("sam", "password");
});

having unsatisfied requests which you can see in this jsfiddle

Error: Unsatisfied requests: GET users/login

If you comment out

$httpBackend.verifyNoOutstandingExpectation() 

your first three tests pass but the last one is amber as there is no expectations, see this fiddle.

WEBSERVICE:
should EXISTS
should throw a WebserviceError if we are not logged in
should NOT HAVE credentials when instantiated
SPEC HAS NO EXPECTATIONS should log in when valid credentials are given

In the AngularJS documentation it says

verifyNoOutstandingExpectation();

Verifies that all of the requests defined via the expect api were made. If any of the requests were not made, verifyNoOutstandingExpectation throws an exception.

You will need to restructure that test so that

webservice.withCredentials("sam", "password");

makes a request through $httpBackend

Nicholas Murray
  • 13,305
  • 14
  • 65
  • 84
  • Thanks a lot for that, i'm not sure how to get `webservice.withCredentials("sam", "password");` to make the request through `$httpBackend` though... – blemaire May 07 '15 at 07:45
  • $httpBackend is a Fake HTTP backend implementation suitable for unit testing applications that use the $http service. So you will be using $http.get in your webService object/service and mocking and testing with $httpBackend. Have a look at the example in the docs https://docs.angularjs.org/api/ngMock/service/$httpBackend – Nicholas Murray May 07 '15 at 12:39
  • actually you need to call $httpBackend.flush() to resolve pending requests and that also evaluates the expectations – mizuki nakeshu Aug 03 '17 at 15:28