簡體   English   中英

AngularJS - 在jasmine測試用例中模擬ngTableParams

[英]AngularJS - mocking ngTableParams in jasmine test case

我已經使用ng-table創建了一個應用程序,該應用程序工作正常,但當我寫一個茉莉花測試用例時,我得到了。

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

任何人都可以告訴我如何模擬ngTableParams並測試其功能

我的代碼如下所示

茉莉花測試案例

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

腳本

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

工作站/ 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
    };
});

首先,確保您的應用依賴於ngTable。 它在'主'里面嗎?

現在進行測試:

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

請注意您必須如何顯式提供每個依賴項作為注入器的參數。

編輯:如果您不想測試與$ scope.tableParams相關的任何內容,igorzg解決方案也會起作用

另一個編輯:你需要讓angular知道注入你的控制器的內容:

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

另一個問題是,在您的測試中,您正在加載wsd.workstations模塊,該模塊沒有注入ngTable。 所以你需要:

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

或者在你的測試中:

beforeEach(module('wsd'));

代替:

beforeEach(module('wsd.workstations'));

只需在創建控制器實例時進行模擬

    function MyNgTableParamsMock() {

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM