2013-04-03 12 views
5

Ho appena iniziato con angolare e volevo scrivere alcuni semplici test unitari per i miei controller, ecco cosa ho ottenuto.angularjs - testing controller

app.js:

'use strict'; 


// Declare app level module which depends on filters, and services 
angular.module('Prototype', ['setsAndCollectionsService']). 
    config(['$routeProvider', function($routeProvider) { 
    $routeProvider.when('/dashboard', {templateUrl: 'partials/dashboard.html', controller: 'DashboardController'}); 
    $routeProvider.when('/setsAndCollections', {templateUrl: 'partials/setsAndCollections.html', controller: SetsAndCollectionsController}); 
    $routeProvider.when('/repetition', {templateUrl: 'partials/repetition.html', controller: RepetitionController}); 
    $routeProvider.otherwise({redirectTo: '/dashboard'}); 
    }]); 

e controllers.js

'use strict'; 

/* Controllers */ 

    var myApp = angular.module('Prototype'); 

    myApp.controller('DashboardController', ['$scope', function (scope) { 
     scope.repeats = 6; 
    }]); 

/*function DashboardController($scope) { 
$scope.repeats = 5; 
};*/ 

function SetsAndCollectionsController($scope, $location, collectionsService, repetitionService) { 
    $scope.id = 3; 
    $scope.collections = collectionsService.getCollections(); 
    $scope.selectedCollection; 
    $scope.repetitionService = repetitionService; 

    $scope.switchCollection = function (collection) { 
     $scope.selectedCollection = collection; 
    }; 

    $scope.addCollection = function() { 
     $scope.collections.push({ 
      name: "collection" + $scope.id, 
      sets: [] 
     }); 
     ++($scope.id); 
    }; 

    $scope.addSet = function() { 
     $scope.selectedCollection.sets.push({ 
      name: "set" + $scope.id, 
      questions: [] 
     }); 
     ++($scope.id); 
    }; 

    $scope.modifyRepetition = function (set) { 
     if (set.isSelected) { 
      $scope.repetitionService.removeSet(set); 
     } else { 
      $scope.repetitionService.addSet(set); 
     } 

     set.isSelected = !set.isSelected; 
    }; 

    $scope.selectAllSets = function() { 
     var selectedCollectionSets = $scope.selectedCollection.sets; 

     for (var set in selectedCollectionSets) { 
      if (selectedCollectionSets[set].isSelected == false) { 
       $scope.repetitionService.addSet(set); 
      } 
      selectedCollectionSets[set].isSelected = true; 
     } 
    }; 

    $scope.deselectAllSets = function() { 
     var selectedCollectionSets = $scope.selectedCollection.sets; 

     for (var set in selectedCollectionSets) { 
      if (selectedCollectionSets[set].isSelected) { 
       $scope.repetitionService.removeSet(set); 
      } 
      selectedCollectionSets[set].isSelected = false; 
     } 
    }; 

    $scope.startRepetition = function() { 
     $location.path("/repetition"); 
    }; 
} 

function RepetitionController($scope, $location, repetitionService) { 
    $scope.sets = repetitionService.getSets(); 
    $scope.questionsLeft = $scope.sets.length; 
    $scope.questionsAnswered = 0; 
    $scope.percentageLeft = ($scope.questionsLeft == 0 ? 100 : 0); 

    $scope.endRepetition = function() { 
     $location.path("/setsAndCollections"); 
    }; 
} 

ora sono nel processo di conversione di controllori funzione globali a quelli definiti da API angolare come si può vedere dall'esempio di DashboardController .

Ora nel mio test:

describe("DashboardController", function() { 
    var ctrl, scope; 

    beforeEach(inject(function ($rootScope, $controller) { 
     scope = $rootScope.$new(); 
     ctrl = $controller('DashboardController', {$scope: scope}); 
    })); 

    it("has repeats attribute set to 5", function() { 
     expect(scope.repeats).toBe(5); 
    }); 
}); 

sto ottenendo

Error: Argument 'DashboardController' is not a function, got undefined 

Mi chiedo allora, dov'è il mio errore? Se ho capito bene, lo ctrl = $controller('DashboardController', {$scope: scope}); dovrebbe iniettare il mio ambito appena creato sul mio DashboardController per popolare con gli attributi - in questo caso, repeats.

+0

Stai assicurandoti di includere controller.js nel runner di test? Ad esempio nel file di configurazione del tuo karma-runner? – Xesued

+0

Ho controllato, il file viene letto OK, ciò che è interessante è che, se annullo lo stile di funzione globale di 'DashboardController', questo semplice test passa così questo file dovrebbe essere incluso. – Andna

+3

Per quello che vale non dovresti appendere gli elementi alla tua variabile $ scope. $ scope dovrebbe contenere un REFERENCE per il tuo modello, ma NON è il tuo modello. per esempio. '$ scope.id' dovrebbe essere qualcosa come' $ scope.SetsAndCollectionsModel.id'. La ragione di ciò deriva dai problemi che cercano di impostare i tipi primitivi da un ambito figlio a un ambito genitore. Puoi guardare un video che descrive questo [qui] (http://www.egghead.io/video/DTx23w4z6Kc). Misko, che ha creato Angular, ne parla in un video [best practice] (http://www.youtube.com/watch?v=ZhfUv0spHCY) pure (mi dispiace, non ho un link direttamente all'ora) –

risposta

19

È necessario impostare prima il modulo Prototype.

beforeEach(module('Prototype')); 

Aggiungi che per il test, al di sopra della corrente beforeEach avrebbe funzionato.

describe("DashboardController", function() { 
    var ctrl, scope; 

    beforeEach(module('Prototype')); 

    beforeEach(inject(function ($rootScope, $controller) { 
    scope = $rootScope.$new(); 
    ctrl = $controller('DashboardController', {$scope: scope}); 
    })); 

    it("has repeats attribute set to 5", function() { 
    expect(scope.repeats).toBe(5); 
    }); 
}); 
+0

Grazie, quello era il frammento mancante. – Andna

Problemi correlati