S
S
Sergey Nikolaev2015-08-10 18:04:11
JavaScript
Sergey Nikolaev, 2015-08-10 18:04:11

How to test controller template in Angular using vm?

Let's say there is a controller in which everything works through this

angular
        .module('app')
        .controller('MyCtrl', MyCtrl);

    /* @ngInject */
    function MyCtrl(){
        var vm = this;
        vm.func = function(){
        }
/*
...
*/
    }

I am writing a test:
it('my ctrl', function () {
        var $scope = $rootScope.$new();
        var vm = $controller('MyCtrl',{$scope:$scope});
        /*
        тут через vm можно как угодно тестировать контроллер
       */
    });

And how to test the template of this controller?
I can't compile the data template because vm is a $controller instance, not a $scope,
so $compile($templateCache.get(pathToTemplate))(vm) can't be done.
How to compile the template in this case?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-08-10
@Devastor

It's simple - don't test controllers. In principle, you should not have any code there that you would like to test. Controllers as separate entities have no templates at all. The only case where this is allowed is in directive controllers, here we have certain templates. But again, here we will test exactly the directive.
You can also test the controller directly by pulling its methods, which is preferable, since with the controller as syntax we get rid of such a harmful dependency as $scope.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question