angularjs - Jasmine/Karma not finding Angular modules -
i'm trying unit testing jasmine , karma, reason angular modules cannot found. i've modified code examples:
karma.config.js:
files: [ 'lib/angular.js', 'lib/angular-mocks.js', 'js/app.js', 'js/controllers.js', 'js/factories.js', 'tests/**/*.js' ]
app.js:
var app = angular.module('app', ['ngroute']); app.config(function ($routeprovider) { $routeprovider .when('/test', { templateurl: 'views/test.html', controller: 'testctrl' }) .otherwise({redirectto: '/'}); });
controllers.js:
app.controller('testctrl', function ($scope, $location) { console.log('test controller'); $scope.isactive = function(route) { return route === $location.path(); }; });
test-spec.js:
describe('testctrl testing', function () { var scope, $location, createcontroller; beforeeach(inject(function ($rootscope, $controller, _$location_) { $location = _$location_; scope = $rootscope.$new(); createcontroller = function () { return $controller('testctrl', { '$scope': scope }); }; })); it('should...', function () { var controller = createcontroller(); $location.path('/test'); expect($location.path()).tobe('/test'); expect(scope.isactive('/test')).tobe(true); expect(scope.isactive('/contact')).tobe(false); }); });
error message: error: [ng:areq] argument 'testctrl' not function, got undefined
i tried with: beforeeach(module('testctrl')), did not help.
what have missed?
there 2 problems can see. in describe section there must main module injection:
beforeeach(module('app'));
the second problem forgot add nganimate module karma files config array:
files: [ 'lib/angular.js', 'lib/angular-route.js', // <-- guy 'lib/angular-mocks.js', ... ]
Comments
Post a Comment