2014-10-08 91 views
6

ngTableParams我一直在使用NG表創建的應用程序,該應用程序是workign正常,但當我寫了一個茉莉花測試情況下,我得到。AngularJS - 嘲諷茉莉測試用例

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

回答

2

首先實例,請確保您的應用程序依賴於ngTable。它在'主'內嗎?

現在的測試:

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

注意你必須明確如何提供每個依賴作爲噴油器的參數。

編輯:igorzg解決方案將工作太多,如果你不想與測試$ scope.tableParams

另一個編輯相關的事情:你需要讓角知道如何注入到你的控制器:

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

另一個問題是,在測試你加載wsd.workstations模塊,已經ngTable注入。所以,你需要:

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

在您的測試OR:中

beforeEach(module('wsd')); 

代替:

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

沒有一分鐘的變化....實際上ng-table是在main.js中定義的...............我們是那個孩子,我已經添加了ng路由和完整的腳本 – 2014-10-10 13:23:02

+0

也不僅嘲笑我想測試的ASC功能 – 2014-10-10 13:37:38

+0

我編輯了答案。你的意思是'asc功能'? – kihu 2014-10-10 13:45:50

1

只是嘲笑它,而你創建控制器

function MyNgTableParamsMock() { 

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

我得到的ReferenceError:可以'找不到變量:$過濾 – 2014-10-10 13:06:59

+0

$過濾器應該始終存在,因爲它的角度api的一部分,使其可行,你可以刪除過濾器注入和模擬過濾器與一些空功能,但過濾器應始終注射。你能粘貼你的代碼嗎? – igorzg 2014-10-15 12:17:53