在茉莉花/羯磨中努力进行单元测试.我有一个具有服务依赖关系的控制器,该服务具有另一个服务依赖关系.我没有按类型(指令,服务等)组织我的模块,而是通过功能(布局,summaryView等)组织我的模块.
这是架构:
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']);
控制器:
angular.module('myApp.layout') .controller('LayoutCtrl',['$scope','$rootScope','$timeout','layoutService','urlService','BaseService',function ($scope,$rootScope,$timeout,layoutService,urlService,BaseService) { //controller code here });
布局服务:
angular.module('myApp.layout') .service('layoutService',['$http','$resource','$location','$route','errorHandlingService','utilService',function ($http,$resource,$location,$route,errorHandlingService,utilService) { //service code here });
从我的理解,如果我只是包括beforeEach(module(‘myApp.layout’));我应该可以访问我的控制器,服务,过滤器和指令在我的布局模块.
相反,以下代码失败:
describe('Layout Controller',function() { var ctrl,scope,service; beforeEach(module('myApp')); beforeEach(module('myApp.layout')); beforeEach(inject(function($controller,layoutService) { scope = $rootScope.$new(); service = layoutService; //Create the controller with the new scope ctrl = $controller('LayoutCtrl',{$scope: scope,layoutService: service}); dump(scope); })); it('should exist',function() { expect(ctrl).toBeDefined(); }); });
有了这个错误:
Chrome 26.0 (Mac) Layout Controller should exist Failed Error: Unknown provider: layoutServiceProvider <- layoutService at Error (<anonymous>) at http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:28:236 at Object.c [as get] (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:26:13) at http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:28:317 at c (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:26:13) at Object.d [as invoke] (http://ajax.googleapis.com/ajax/libs/angularjs/1.0.4/angular.min.js:26:147) at workFn (http://code.angularjs.org/1.0.4/angular-mocks.js:1754:20) Error: Declaration Location at window.jasmine.window.inject.angular.mock.inject (http://code.angularjs.org/1.0.4/angular-mocks.js:1740:25) at null.<anonymous> (/Users/scottsilvi/svn/BARO/web/src/test/js/unit/myApp.layoutModule.js:6:14) at /Users/scottsilvi/svn/BARO/web/src/test/js/unit/myApp.layoutModule.js:1:1 Expected undefined to be defined. Error: Expected undefined to be defined. at 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)
思考?