javascript - AngularJS - mocking ngTableParams in jasmine test case -
i have created application using ng-table , application workign fine when wrote jasmine test case getting.
error: [$injector:unpr] unknown provider: tableparamsprovider
can please tell me how mock ngtableparams
, test functionality
my code given below
jasmine test case
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() 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) { homecoming documents.getworkdocuments(); } } }).when('/wp', { templateurl: 'main/workperiod/main.tpl.html', controller: 'periodcontroller', resolve: { myworkperiods: function(periods) { homecoming periods.getworkperiods(); } } }).otherwise({ redirectto: '/wd' }); });
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() { // other logic }; });
first, create sure app depends on ngtable. within 'main'?
now test:
beforeeach(inject(function($controller, $rootscope, $filter, ngtableparams) { $scope = $rootscope.$new(); workcontroller = $controller('workcontroller', { $rootscope: $rootscope, $scope: $scope, $filter: $filter, ngtableparams: ngtableparams }); }));
notice how must explicitly provide every dependency parameter injector.
edit: igorzg solution work if don't want test associated $scope.tableparams
another edit: need allow angular know inject controller:
.controller('workcontroller', ['$rootscope', '$scope', '$filter', 'ngtableparams', function($rootscope, $scope, $filter, ngtableparams) { // controller code here }]);
another problem in test you're loading wsd.workstations module, doesn't have ngtable injected. need to:
angular.module('wsd.workstations', ['ngtable'])
or in test:
beforeeach(module('wsd'));
instead of:
beforeeach(module('wsd.workstations'));
javascript angularjs jasmine karma-jasmine ngtable
No comments:
Post a Comment