14

努力在 Jasmine/Karma 中设置单元测试。我有一个具有服务依赖关系的控制器,并且该服务具有另一个服务依赖关系。我不是按类型(指令、服务等)组织我的模块,而是按功能(布局、摘要视图等)组织我的模块。

这是架构:

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', '$rootScope', '$location', '$route', 'errorHandlingService', 'utilService',
        function ($http, $resource, $rootScope, $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, $rootScope, 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)

想法?

4

2 回答 2

10

未知提供程序错误通常来自未加载的文件,或以不正确的顺序加载。在测试运行时检查哪些文件存在。

于 2013-05-20T23:29:19.087 回答
1

此类错误主要是由于karma.config.js文件中的文件声明不正确造成的。对于karma-jasmine的设置, karma.config.jsfile:[]文件块中的所有文件应该按预期加载的顺序排列。主模块正在对任何自定义服务进行依赖注入,然后先加载该服务,然后再加载主模块。

于 2015-04-20T09:04:03.803 回答