sorting feature in ngTable using Jasmine Testing

帅比萌擦擦* 提交于 2019-12-06 12:08:45

问题


I have created an application using ng-table , the application is working fine but i don'nt know how to write a test case for that sorting and getData.

Can anyone please tell me some solution for testing that 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'));

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

        it('should tableParams when tableParams is called', function() {

        });
    });
});

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

Update 2

I have done like this for testing, but getting

<failure type="">TypeError: &apos;undefined&apos; is not a function (evaluating &apos;$defer.resolve($scope.myValues)&apos;)

test cases

    it('should check tableParams getData sorting', inject(function($q) {
            var deferred = $q.defer(); 
            var promise = deferred.promise;
            promise.then(function(result) {
               expect(result).toEqual(expectedResult);
            });
             $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.getData(promise, $scope.tableParams );
}));

回答1:


You could:

Declare getData function on controller's $scope, thus making it available in your test:

$scope.tableParams = new ngTableParams({
        sorting: {
            name: 'asc'     
        }
    }, {
        getData: $scope.getData
    });

$scope.getData = function($defer, params) {
            $scope.myValues = $filter('orderBy')($scope.myValues, params.orderBy());
            $defer.resolve($scope.myValues);
        }

Inject $q in beforeEach().

Create a promise object using $q.

Assign some $scope.myValues for your unit test.

Declare a variable containing your expected result - that is your sorted $scope.myValues array. Then:

promise.then(function(result){
    expect(result).toEqual(expectedResult);
}
$scope.getData(deferred , $scope.tableParams);


来源:https://stackoverflow.com/questions/26301572/sorting-feature-in-ngtable-using-jasmine-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!