mercredi 26 août 2015

Testing angular controller with many dependencies

I've started testing my Angular app and have question that bother me a lot. For example I have controller (mainController) which is injecting 2 services: authService, configService.

Before testing I should prepare something like that:

describe('controller: testController with testService mock', function() {      
    var controller, authService, configService;

    beforeEach(module('app'));

    beforeEach(inject(function($controller, _authService_, _configService_) {         
        authService = _authService_;
        configService = _configService_;

        controller = $controller('mainController');
    }));    

    it('should be registered with all dependencies', function() {
        expect(controller).to.be.defined;

        expect(authService).to.be.defined;
        expect(configService).to.be.defined;
    });

}

And that's totally clear for me. What if one of services or both have their own dependencies (services) ? Of course I'm gonna add it by passing through the inject function. In small apps that's no big problem. I'm adding as much services as I need. But the question is what if that services are injecting other services and others injecting others and there is a huge hierarchy ? What if we must add 30 services and we can't make a mistake because otherwise it's not gonna work ?

To be honest I've search a lot but there are many testing examples and tutorials but every single one is based on totally basic apps with few controllers and services.

Is there a painless way to handle this ? Maybe there is a way to skip some dependencies or force to automatically inject services with it's dependencies ?

Aucun commentaire:

Enregistrer un commentaire