2016-07-11 23 views
7

ho questo semplice test:Perché il mio test di karma non funziona?

describe('My Controller', function() { 

    beforeEach(function() { 
    module('myApp'); 
    return inject(function($injector) { 
     var $controller = $injector.get('$controller'); 
     this.rootScope = $injector.get('$rootScope'); 
     this.scope = this.rootScope.$new(); 

     this.controller = $controller('MyCtrl', { 
     '$scope': this.scope, 
     }); 
    }); 
    }); 

    it('should have a controller', function() { 
    expect(this.controller).toBeDefined(); 
    }); 
}); 

il controller è simile al seguente:

angular.module('myApp').controller('MyCtrl', ['$scope', '$state', '$filter', '$q', 'BookingService', 'ngToast', '$uibModal', 
function($scope, $state, $filter, $q, BookingService, ngToast, $uibModal) { 

    $scope.bs = BookingService; 
    $scope.roundTrip = false; 
    $scope.reservationDetails = {}; 
    $scope.originAddress = false; 
    $scope.destinationAddress = false; 
    $scope.reservationDetails.roundTrip = false; 
    $scope.seatReservationDepart = {}; 
    $scope.charter = false; 
}]); 

Il test mantiene in mancanza e il terminale non è in realtà dando tutte le informazioni utili sul perché.

+2

Si potrebbe andare a http: // localhost: 9876/debug.html, questo vi permetterà di eseguire il debug nella console, come si farebbe normalmente che preferisco sul terminale. A supposizione di ciò che va storto, il tuo controller ha un sacco di dipendenze che non fornisci nei tuoi test. - edit: prova ad aggiungere lo stack degli errori, perché il debugging è molto difficile :) – Pjetr

risposta

0

Non utilizzare this nel test, si riferisce a cose diverse in diverse parti della suite.

Invece, inizializzare un ambito "contenitore" nel describe namespace:

describe('My Controller', function() { 
    var scope = {}; 

    beforeEach(function() { 
    module('myApp'); 

    return inject(function($injector) { 
     var $controller = $injector.get('$controller'); 
     this.rootScope = $injector.get('$rootScope'); 
     this.scope = this.rootScope.$new(); 

     scope.controller = $controller('MyCtrl', { 
     '$scope': this.scope, 
     }); 
    }); 
    }); 

    it('should have a controller', function() { 
    expect(scope.controller).toBeDefined(); 
    }); 
});