Search code examples
angularjshttpbackend

AngularJS testing $httpBackend.whenGET().respond() not working with number as parameter


This test:

it("getFooCount success", function () {
    httpBackend.whenGET('/api/getFooCount').respond(1);

    fooService.getFooCount();
    httpBackend.flush();
    expect(fooService.FooCount).toBe(1);
});

fails: "Expected null to be 1" (fooService.fooCount is null before the api get method is called).

Yet this test passes:

it("getFooCount success", function () {
    httpBackend.whenGET('/api/getFooCount').respond("1");

    fooService.getFooCount();
    httpBackend.flush();
    expect(fooService.fooCount).toBe("1");
});

It seems to be able to pass the test with anything (an object, string, array, etc) EXCEPT a number in the .respond().

The api I am calling just returns an integer, any advice as to how I can get this to work, and/or why it is failing?


Solution

  • Have a look at the API for respond, because the first argument is actually the status code.

    Try instead respond(200, 1).

    Angularjs seems to smart its way around when you provide a non statist object as first parameter