HttpBackend only waits for a request once

After a bit of trial and error, I was finally able to get Jasmine to inject the Angular layout, and also provide $httpBackend

in the same package:

describe('services.system', function () {

    var httpBackend, nodeService;

    beforeEach(module('services.system'));

    beforeEach(function() {
        module(function($provide) {
            $provide.factory('authService', function() { return mockAuthService; });
        });

        inject(function(_nodeService_, $httpBackend) {
            httpBackend = $httpBackend;
            nodeService = _nodeService_;
        });
    });

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

    describe('version', function () {
        it('should return the current version', function () {
            httpBackend.expectGET('/service/version').respond(200, { version: "1.2.3"});

            var done = false;
            runs(function() {
                nodeService.getProductInfo().then(function (info) {
                    expect(info.version).toEqual('1.2.3');
                    done = true;
                }, function(error) {
                    expect("test received error: " + error).toFail();
                    done = true;
                });

                httpBackend.flush();
            });

            waitsFor(function(){
                return done;
            });
        });
    });
});

      

Now I want to write a test that expects a GET only once, two consecutive calls should return a cached response and not make another request to the server. With spies, it looks like you usually do this by claiming myObj.calls.count()

.

How can I verify that the HTTP request was only called once?

+3


source to share





All Articles