Pregunta

I have an angular app that i am developing tests for. In the app.js file i have something like this:

angular.module('app',
[
'app.config',
'app.factories',
'app.directives',
'app.controllers'
]
);

For each controller i want to be in that controller module i essentially define them like this:

angular.module('app.controllers').controller("controller1" ,[],function(){
bleh bleh bleh
code code code 
})

The goal here is to write some unit tests with karma but unfortunately the most i have been able to figure out how to do is make sure the dependencies of my main modules load.

What I need to figure out is using the structure i have, how do I (a) create a test to make sure that my controller is actually there, and (b) test things inside the controller

I have tried multiple ways but cannot seem to instantiate the controller within my test framework.

¿Fue útil?

Solución

You can test for the existence of your controller like this:

describe("SomeControllerTest", function () {
    var scope, ctrl;

    beforeEach(module('myApp'));

    beforeEach(inject(function($rootScope, $controller) {
        scope = $rootScope.$new();
        ctrl = $controller('SomeController', {
            $scope: scope
        });
    }));

    it("should be defined", function () {
        expect(ctrl).toBeDefined();
    });
});

Careful with your controller syntax. The second param is an array of strings ending with the function, the function is not a 3rd param.

app.controller('MyController', [ '$log', function MyController($log) {} ]);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top