How to test controller created via module.config in angularjs -
i have controller defined in following manner, works perfect. when try test controller says "(controller name)" not function, got undefined". how can test controller defined way.
controller.js
var mainmodule = angular.module('module1'); function home($scope) { $scope.test = "hello"; } home.$inject = ["$scope"]; mainmodule.config(['$routeprovider', function ($routeprovider) { $routeprovider.when('/', { templateurl: 'partials/home.html', controller: home }); } ]);
testspec.js
describe("test home controller", function () { beforeeach(module('module1')); it("test controller ", inject(function ($rootscope, $controller) { var ctrl = $controller("home", { $scope: $rootscope }); expect($rootscope.items.length).tobe(3); })); });
as mentioned in comments @paolomoretti, shouldn't define controller in global scope (unless prototyping). instead definie controller part of module:
var mainmodule = angular.module('module1'); mainmodule.controller('home', function($scope) { $scope.test = "hello"; });
then, when use line in test:
beforeeach(module('module1'));
the controller available testing.
Comments
Post a Comment