evangalen / ng-improved-testing

Improves AngularJS testing
MIT License
21 stars 4 forks source link

$httpBackend doesn't work with mocks enabled #15

Closed harjup closed 9 years ago

harjup commented 9 years ago

I'm trying to test a service that has dependencies on external services as well as $http. When I test http requests through $httpBackend, it doesn't work.

If I don't exclude $http, a mock object is generated. If I do, it's not wired up to $httpBackend correctly and requests are not properly flushed.

Here are two tests illustrating the issue, the first one does not work, while the second one does:

describe('$httpBackend', function() {
    describe('with mocked services', function(){

        beforeEach(ModuleBuilder.forModule('myApp')
            .serviceWithMocksExcept('userService', '$http')
            .build());

        it('should work', inject(function(userService, $httpBackend) {
            //userService automatically does an $http call on startup
            $httpBackend.expectGET('/users').respond(200);
            $httpBackend.flush();
        }));
    });

    describe('withoutMocks', function(){
        beforeEach(module('myApp'));

        it('should work', inject(function($httpBackend, userService) {
            //userService automatically does an $http call on startup
            $httpBackend.expectGET('/users').respond(200);
            $httpBackend.flush();
        }));
    });
});

Result:

$httpBackend with mocked services should work FAILED
        Error: No pending request to flush !
        at Function.$httpBackend.flush (.../angular-mocks.js:1452:34)
        remaining stack trace....
evangalen commented 9 years ago

Thanks you for filing the bug and making is so easiliy reproducible :-)

You issue has be fixed with the recent release of 0.3.0-beta.3 with has much improved ModuleBuilder, more info can be found in the CHANGELOG of the 0.3 branch: http://bit.ly/1AhV2mf Hope to release a final 0.3.0 release within the next few weeks but the 0.3.0-beta.3 pre-release should stable enough to be used right now.