1

我正在使用yeoman angular-require发电机。
https://github.com/aaronallport/generator-angular-require
我的一个控制器使用ng-table. 当对这个控制器进行单元测试时,我收到错误:Unknown provider: ngTableProvider <- ngTable.

我可以ng-table在我的 webapp 中使用,没有任何问题。仅在单元测试用例中,我无法将其注入或添加为模块。请协助。

以下是我的设置:
ng-table 在 requirejs 配置路径中可用。
它也被添加到引导部分。
define(['angular', 'ng-table', 'controllers/main'], function (angular, ngTable, MainCtrl,) { return angular.module('myApp', [ 'ngTable', 'myApp.controllers.MainCtrl ', 'ngCookies', 'ngResource', 'ngSanitize', 'ngRoute' ]).config......

控制器:

define(['angular'], function (angular) {
  'use strict';    
  angular.module('myApp.controllers.MainCtrl', [])
    .controller('MainCtrl', function ($scope, ngTableParams) {
      $scope.awesomeThings = [
        'HTML5 Boilerplate',
        'AngularJS',
        'Karma'
      ];

    $scope.tableParams = new ngTableParams({
      page: 1, 
      count: 5
    }, {
        total: data.length,
        getData: function($defer, params) {
          $defer.resolve(data.slice((params.page() - 1) * params.count(), params.page() * params.count()));
        }
    });

    });
});

测试设置:
ng-table在 requirejs 配置路径中可用。

主要规范文件:

define(['angular', 'angular-mocks', 'ng-table', 'app'], function(angular, mocks, ngTable, app) {
  'use strict';

  describe('Controller: MainCtrl', function () {

    // load the controller's module
    beforeEach(module('scottsApp.controllers.MainCtrl'));

   //Not sure how to inject ngTable
   //tried beforeEach(module('ngTableParams'));
   //tried beforeEach(module('ngTable'));
   //tried beforeEach(inject(function () {}));
   //Nothing is working....

    var MainCtrl,
      scope;

    // Initialize the controller and a mock scope
    beforeEach(inject(function ($controller, $rootScope) {
      scope = $rootScope.$new();
      MainCtrl = $controller('MainCtrl', {
        $scope: scope
      });
    }));

    it('should attach a list of awesomeThings to the scope to check controller load', function () {
      expect(scope.awesomeThings.length).toBe(3);
    });
  });
});
4

1 回答 1

0

确保在 karma.conf.js 中包含 ng-table。这将解决你的问题。

// list of files / patterns to load in the browser
files: [
    'bower_components/angular/angular.js',
    'bower_components/angular-mocks/angular-mocks.js',
    'bower_components/angular-animate/angular-animate.js',
    'bower_components/angular-route/angular-route.js',
    'bower_components/angular-sanitize/angular-sanitize.js',
    'bower_components/ng-table/ng-table.js',
    'app/scripts/**/*.js',
    'test/mock/**/*.js',
    'test/spec/**/*.js'
],
于 2014-11-18T06:59:16.060 回答