AngularJS - mocking ngTableParams in jasmine test case
Asked Answered
L

2

6

I have created an application using ng-table , the application is workign fine but when i wrote a jasmine test case i am getting.

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

Can anyone please tell me how to mock the ngTableParams and test its functionality

My code is as 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() 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'
    });
});

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
    };
});
Lactalbumin answered 8/10, 2014 at 11:29 Comment(0)
C
2

First, make sure that you app depends on ngTable. Is it inside 'main'?

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

Notice how you must explicitly provide every dependency as a parameter for injector.

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

another edit: You need to let angular know what to inject into your controller:

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

Another problem is that in your test you're loading wsd.workstations module, which doesn't have ngTable injected. So you need to:

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

OR in your test:

beforeEach(module('wsd'));

instead of:

beforeEach(module('wsd.workstations'));
Codify answered 10/10, 2014 at 12:0 Comment(9)
No there's a minute change ....actually ng-table is defined within the main.js.............ours is the child to that, i have added the ng-route and the full scriptLactalbumin
also not only mocking i want to test the asc functionality tooLactalbumin
I've edited the answer. What do you mean 'asc functionality'?Codify
can we test that orderBy sortingLactalbumin
I already see that sorting won't work because you're trying to use $defer but you don't pass it to your controller.Codify
Oh, my bad. I didn't notice that $defer is injected by ngTable.Codify
The question is about mocking ngTableParams, not writing a test for sorting.Codify
i have wrote a seperate question for that #26302072 can you please tell me some solution for thatLactalbumin
Can you give me some solution for that sorting test casesLactalbumin
M
1

Just mock it while you create instance of controller

    function MyNgTableParamsMock() {

    } 
    beforeEach(inject(function($controller, $rootScope, $filter) {
        $scope = $rootScope.$new();
        WorkController = $controller('WorkController', {
            $rootScope: $rootScope,
            $scope: $scope,
            $filter: $filter,
            ngTableParams: MyNgTableParamsMock  
        });
    }));
Mcinerney answered 10/10, 2014 at 11:57 Comment(2)
i am getting ReferenceError: Can't find variable: $filterLactalbumin
$filter should be always there because its part of angular api, to make it workable you can remove filter from inject and mock filter with some empty function but filter should be always injectable. can you paste your code ?Mcinerney

© 2022 - 2024 — McMap. All rights reserved.