No pending request to flush on using whenGET, but not expectGET

712 views Asked by At

When testing an angular service using $httpBackend I get some unexpected results.

When I run the test using httpBackend.expectGET the test works as expected. However if I run the exact same test using whenGET the test fails with the message 'No pending request to flush'.

Here is the service under test:

 .factory('CoverageService', ['$http', function ($http) {
     return{
         GetCoverageReport: function () {
             return $http.get('../../../js-test-reports/coverage/Chrome 43.0.2357%20(Windows%207)/cobertura-coverage.xml');
     },

Here are the tests:

'use strict';

describe('coverage-manager.js', function () {
    describe('CoverageService', function () {
        var httpBackend, sut, rootScope;
        beforeEach(module('fot'));
        describe('GetCoverageReport', function () {
            beforeEach(inject(function ($httpBackend, CoverageService, $rootScope) {
                httpBackend = $httpBackend;
                sut = CoverageService;
                rootScope = $rootScope;
            }));
                
            it('should return data', function () {
                var expectedData = { testData: 'some data' };
                var actualData;
                httpBackend.expectGET('../../../js-test-reports/coverage/Chrome 43.0.2357%20(Windows%207)/cobertura-coverage.xml').respond(expectedData);

                sut.GetCoverageReport()
                    .success(function (response) { actualData = response; });
                httpBackend.flush();

                expect(actualData).toEqual(expectedData);
            });
            it('should return data but says No pending request to flush', function () {
                var expectedData = { testData: 'some data' };
                var actualData;
                httpBackend.whenGET('../../../js-test-reports/coverage/Chrome 43.0.2357%20(Windows%207)/cobertura-coverage.xml').respond(expectedData);

                sut.GetCoverageReport()
                    .success(function (response) { actualData = response; });
                rootScope.$digest();
                httpBackend.flush();

                expect(actualData).toEqual(expectedData);
            });
            it('should return data', function () {
                var expectedData = { testData: 'some data' };
                var actualData;
                httpBackend.whenGET('../../../js-test-reports/coverage/Chrome 43.0.2357%20(Windows%207)/cobertura-coverage.xml').respond(expectedData);

                sut.GetCoverageReport()
                    .success(function (response) { actualData = response; });
                httpBackend.flush();

                expect(actualData).toEqual(expectedData);
            });
        });
    });
});

The expectGET passes, both whenGETs fail with "No pending request to flush" message. I tried using the $rootScope.$digest() as I saw in some posts, no luck. The tests are identical except the expectGET vs expectWHEN, so not sure what is going on?

And here is the error detail:

Error: No pending request to flush !
at Function.$httpBackend.flush (file:///C:/dev/fot/git/client/src/main/js/bower_components/angular-mocks/angular-mocks.js:1455:34)
at Object.<anonymous> (file:///C:/dev/fot/git/client/src/test/javascript/spec/controllers/devTools/CoverageManager.spec.js:78:21)
at attemptSync (file:///C:/dev/fot/git/client/src/main/js/node_modules/karma-jasmine/lib/jasmine.js:1510:12)
at QueueRunner.run (file:///C:/dev/fot/git/client/src/main/js/node_modules/karma-jasmine/lib/jasmine.js:1498:9)
at QueueRunner.execute (file:///C:/dev/fot/git/client/src/main/js/node_modules/karma-jasmine/lib/jasmine.js:1485:10)
at Spec.queueRunnerFactory (file:///C:/dev/fot/git/client/src/main/js/node_modules/karma-jasmine/lib/jasmine.js:518:35)
at Spec.execute (file:///C:/dev/fot/git/client/src/main/js/node_modules/karma-jasmine/lib/jasmine.js:306:10)
at Object.<anonymous> (file:///C:/dev/fot/git/client/src/main/js/node_modules/karma-jasmine/lib/jasmine.js:1708:37)
at attemptAsync (file:///C:/dev/fot/git/client/src/main/js/node_modules/karma-jasmine/lib/jasmine.js:1520:12)
at QueueRunner.run (file:///C:/dev/fot/git/client/src/main/js/node_modules/karma-jasmine/lib/jasmine.js:1496:16)
  • angular 1.2.28
  • angular-mocks 1.2.28
  • jasmine 2.0.0
0

There are 0 answers