AngularJS - mocking ngTableParams in jasmine test case

I created an application using ng-table, the application works fine, but when I wrote a jasmine test case, I get.

Error: [$injector:unpr] Unknown provider: TableParamsProvider 

Can someone tell me how to mock ngTableParams and check its functionality

My code is below

jasmine test

 describe('Testing Controllers', function() { describe('Testing WorkController Controller', function() { var WorkController, $scope; beforeEach(module('wsd.workstations')); beforeEach(inject(function($controller, $rootScope) { $scope = $rootScope.$new(); WorkController = $controller('WorkController', { $rootScope: $rootScope, $scope: $scope }); })); it('should searchDocuments when searchDocuments() is called', function() { $scope.searchDocuments(); }); }); }); 

script

 angular.module('wsd', ['restangular', 'ngTable', 'wsd.models', 'wsd.workstations', 'wsd.workperiods', 'ngRoute']) .config(function(RestangularProvider, $routeProvider) { RestangularProvider.setBaseUrl('/rest/myuser'); $routeProvider.when('/wd', { templateUrl: 'main/workstation/main.tpl.html', controller: 'WorkController', resolve: { myWorkDocuments: function(Documents) { return Documents.getWorkDocuments(); } } }).when('/wp', { templateUrl: 'main/workperiod/main.tpl.html', controller: 'PeriodController', resolve: { myWorkPeriods: function(Periods) { return Periods.getWorkPeriods(); } } }).otherwise({ redirectTo: '/wd' }); }); 

<strong> workstation /main.js

 angular.module('wsd.workstations', []) .controller('WorkController', function($rootScope, $scope, $filter, ngTableParams) { $scope.myValues = [{name: "Moroni", age: 50}, {name: "Tiancum", age: 43}, {name: "Jacob", age: 27}, {name: "Nephi", age: 29}, {name: "Enos", age: 34}, {name: "Tiancum", age: 43}, {name: "Jacob", age: 27}, {name: "Nephi", age: 29}, {name: "Enos", age: 34}, {name: "Tiancum", age: 43}, {name: "Jacob", age: 27}, {name: "Nephi", age: 29}, {name: "Enos", age: 34}, {name: "Tiancum", age: 43}, {name: "Jacob", age: 27}, {name: "Nephi", age: 29}, {name: "Enos", age: 34}]; $scope.tableParams = new ngTableParams({ sorting: { name: 'asc' } }, { getData: function($defer, params) { $scope.myValues = $filter('orderBy')($scope.myValues, params.orderBy()); $defer.resolve($scope.myValues); } }); $scope.searchDocuments = function() { // some other logic }; }); 
+6
source share
2 answers

First, make sure your application is dependent on ngTable. Is it inside the "core"?

Now for the test:

 beforeEach(inject(function($controller, $rootScope, $filter, ngTableParams) { $scope = $rootScope.$new(); WorkController = $controller('WorkController', { $rootScope: $rootScope, $scope: $scope, $filter: $filter, ngTableParams: ngTableParams }); })); 

Note that you must explicitly specify each dependency as a parameter for the injector.

edit: igorzg solution will work too if you don't want to test anything related to $ scope.tableParams

another edit: you need to tell angular what to enter into the controller:

 .controller('WorkController', ['$rootScope', '$scope', '$filter', 'ngTableParams', function($rootScope, $scope, $filter, ngTableParams) { // your controller code here }]); 

Another problem is that in your test you load the wsd.workstations module, which ngTable is not entered into. Therefore you need to:

 angular.module('wsd.workstations', ['ngTable']) 

OR in your test:

 beforeEach(module('wsd')); 

instead:

 beforeEach(module('wsd.workstations')); 
+2
source

Just scoff at creating a controller instance

  function MyNgTableParamsMock() { } beforeEach(inject(function($controller, $rootScope, $filter) { $scope = $rootScope.$new(); WorkController = $controller('WorkController', { $rootScope: $rootScope, $scope: $scope, $filter: $filter, ngTableParams: MyNgTableParamsMock }); })); 
+1
source

Source: https://habr.com/ru/post/976416/


All Articles