Testing modular AngularJS w/ Jasmine & Karma -
struggling unit testing set in jasmine/karma. have controller service dependency, , service has service dependency. not organizing modules type (directives, services, etc), rather feature (layout, summaryview, etc).
here's architecture:
angular.module('myapp', ['ngresource', 'myapp.base', 'myapp.layout','myapp.common']); angular.module('myapp.base', ['myapp.common']); angular.module('myapp.common',[]); angular.module('myapp.layout',['myapp.common']);
controller:
angular.module('myapp.layout') .controller('layoutctrl', ['$scope', '$rootscope', '$timeout', 'layoutservice', 'urlservice', 'baseservice', function ($scope, $rootscope, $timeout, layoutservice, urlservice, baseservice) { //controller code here });
layout service:
angular.module('myapp.layout') .service('layoutservice', ['$http', '$resource', '$rootscope', '$location', '$route', 'errorhandlingservice', 'utilservice', function ($http, $resource, $rootscope, $location, $route, errorhandlingservice, utilservice) { //service code here });
from understand, if include beforeeach(module('myapp.layout'));
, should have access controllers, services, filters, , directives in layout module.
instead, following code fails:
describe('layout controller', function() { var ctrl, scope, service; beforeeach(module('myapp')); beforeeach(module('myapp.layout')); beforeeach(inject(function($controller, $rootscope, layoutservice) { scope = $rootscope.$new(); service = layoutservice; //create controller new scope ctrl = $controller('layoutctrl', {$scope: scope, layoutservice: service}); dump(scope); })); it('should exist', function() { expect(ctrl).tobedefined(); }); });
with error:
chrome 26.0 (mac) layout controller should exist failed error: unknown provider: layoutserviceprovider <- layoutservice @ error (<anonymous>) @ http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:28:236 @ object.c [as get] (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:26:13) @ http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:28:317 @ c (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:26:13) @ object.d [as invoke] (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:26:147) @ workfn (http://code.angularjs.org/1.0.4/angular-mocks.js:1754:20) error: declaration location @ window.jasmine.window.inject.angular.mock.inject (http://code.angularjs.org/1.0.4/angular-mocks.js:1740:25) @ null.<anonymous> (/users/scottsilvi/svn/baro/web/src/test/js/unit/myapp.layoutmodule.js:6:14) @ /users/scottsilvi/svn/baro/web/src/test/js/unit/myapp.layoutmodule.js:1:1 expected undefined defined. error: expected undefined defined. @ null.<anonymous> (/users/scottsilvi/svn/baro/web/src/test/js/unit/myapp.layoutmodule.js:15:16) chrome 26.0 (mac): executed 10 of 10 (1 failed) (0.36 secs / 0.014 secs)
thoughts?
often unknown provider error comes files not being loaded, or being loaded in incorrect order. check files present while tests running.
Comments
Post a Comment