1
votes

I am using the yeoman angular-require generator.
https://github.com/aaronallport/generator-angular-require
One of my controller uses ng-table. When Unit testing this controller, I am getting the error: Unknown provider: ngTableProvider <- ngTable.

I am able to use the ng-table within my webapp without any issues. Only inside the unit test case, I am unable to inject or add it as a module. Please assist.

Below is my setup:
ng-table is available inside the requirejs config paths.
It is added in the bootstrapping section as well.
define(['angular', 'ng-table', 'controllers/main'], function (angular, ngTable, MainCtrl,) { return angular.module('myApp', [ 'ngTable', 'myApp.controllers.MainCtrl', 'ngCookies', 'ngResource', 'ngSanitize', 'ngRoute' ]). config......

Controller:

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()));
        }
    });

    });
});

TEST SETUP:
ng-table is available inside the requirejs config paths.

mainSpec file:

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);
    });
  });
});
1

1 Answers

0
votes

Make sure you include ng-table in your karma.conf.js. That will solve your problem.

// 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'
],