2014-10-08 15 views
6

Tôi đã tạo một ứng dụng bằng ng-table, ứng dụng này hoạt động tốt nhưng khi tôi viết một trường hợp thử nghiệm hoa nhài tôi đang nhận được.AngularJS - mocking ngTableParams trong trường hợp thử nghiệm hoa nhài

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

bất cứ ai có thể vui lòng cho tôi biết làm thế nào để chế nhạo các ngTableParams và kiểm tra chức năng của nó

Mã của tôi là như đưa ra dưới đây

test nhài

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

kịch bản

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

máy trạm/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 
    }; 
}); 

Trả lời

2

Trước tiên, hãy chắc chắn rằng bạn ứng dụng phụ thuộc vào ngTable. Nó nằm trong 'chính'?

Bây giờ cho các bài kiểm tra:

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

Thông báo như thế nào bạn phải cung cấp một cách rõ ràng tất cả các phụ thuộc như một tham số cho vòi phun.

chỉnh sửa: giải pháp igorzg sẽ làm việc quá nếu bạn không muốn để kiểm tra bất cứ điều gì liên quan đến $ scope.tableParams

chỉnh sửa khác: Bạn cần phải cho góc biết phải tiêm vào điều khiển của bạn:

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

Một vấn đề khác là trong thử nghiệm của bạn, bạn đang tải mô-đun wsd.workstations, trong đó không có ngTable được tiêm. Vì vậy, bạn cần phải:

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

HOẶC trong thử nghiệm của bạn:

beforeEach(module('wsd')); 

thay vì:

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

Không có thay đổi nào cả ... thực sự ng-table được định nghĩa trong main.js ............. chúng ta là đứa trẻ, tôi đã thêm ng-route và toàn bộ kịch bản –

+0

cũng không chỉ chế giễu tôi muốn kiểm tra chức năng asc quá –

+0

Tôi đã chỉnh sửa câu trả lời. Bạn có ý nghĩa gì 'chức năng asc'? – kihu

1

Chỉ cần thử nó trong khi bạn tạo thể hiện của điều khiển

function MyNgTableParamsMock() { 

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

tôi đang nhận được ReferenceError: Có thể ' t tìm biến: $ lọc $ –

+0

bộ lọc nên luôn ở đó vì phần của nó là api góc cạnh, để làm cho nó khả thi, bạn có thể loại bỏ bộ lọc khỏi bộ lọc tiêm và giả với một số hàm rỗng nhưng filte r nên luôn tiêm. bạn có thể dán mã của bạn không? – igorzg

Các vấn đề liên quan