测试模块AngularJS与/ Jasmine&Karma

问题描述 投票:14回答:2

努力在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)

思考?

unit-testing angularjs jasmine karma-runner
2个回答
10
投票

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


0
投票

这种错误主要是由于karma.config.js文件中的文件声明不正确而引起的。对于karma-jasmine的设置,karma.config.js文件的file:[]块内的所有文件都应按顺序加载。主模块具有依赖注入任何自定义服务,然后首先加载该服务,然后加载主模块。

© www.soinside.com 2019 - 2024. All rights reserved.