Answer the question
In order to leave comments, you need to log in
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(){
}
/*
...
*/
}
it('my ctrl', function () {
var $scope = $rootScope.$new();
var vm = $controller('MyCtrl',{$scope:$scope});
/*
тут через vm можно как угодно тестировать контроллер
*/
});
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question