2014-10-08 4 views
6

Ho creato un'applicazione utilizzando ng-table, l'applicazione è workign bene ma quando ho scritto un caso di test al gelsomino sto ottenendo.AngularJS - mocking ngTableParams in test case jasmine

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

Qualcuno può dirmi come per deridere il ngTableParams e testare la sua funzionalità

Il mio codice è come indicato di seguito

banco di prova gelsomino

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

sceneggiatura

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

risposta

2

In primo luogo, assicurarsi che voi dipende app su ngTable. È dentro 'principale'?

Ora per il test:

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

Notate come è necessario fornire esplicitamente ogni dipendenza come parametro per l'iniettore.

edit: soluzione igorzg funzionerà anche se non si vuole testare qualcosa di legato al $ scope.tableParams

un'altra edit: È necessario lasciare angolare sanno cosa iniettare nel controller:

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

Un altro problema è che nel tuo test stai caricando il modulo wsd.workstations, che non ha iniettato. Quindi è necessario:

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

O nel test:

beforeEach(module('wsd')); 

invece di:

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

Non c'è un minuto di cambiamento .... in realtà ng-table è definito all'interno del main.js ............. il nostro è il bambino, ho aggiunto il percorso ng e il full script –

+0

non solo sto prendendo in giro anche io voglio testare la funzionalità asc –

+0

Ho modificato la risposta. Cosa intendi per "funzionalità asc"? – kihu

1

Basta deridono mentre si crea un'istanza di controllo

function MyNgTableParamsMock() { 

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

sto ottenendo ReferenceError: Può ' t trovare variabile: $ filtro –

+0

$ il filtro dovrebbe essere sempre lì perché la sua parte di api angolari, per renderlo praticabile è possibile rimuovere il filtro da iniettare e falsificare il filtro con qualche funzione vuota ma filte dovrebbe essere sempre iniettabile puoi incollare il tuo codice? – igorzg