mardi 27 janvier 2015

AngularJS unit test controller with $locationChangeStart in it

I'm new to AngularJS Unit testing. I want to test a function in a controller with jasmine. In the same controller there is also a watch for $locationChangeStart



$scope.$on('$locationChangeStart', function(){
$scope.killTimer($scope.timer);
});


I do not want to test this, but another function. Still when I try to run a test, I get an error, which points to the $scope.$on line:



TypeError: undefined is not a function


What should I do to make the test not run this line? I copy my test spec because I'm not sure if everything is OK there.



describe('invoiceListController', function() {
beforeEach(module('invoice'));

var $controller;

beforeEach(inject(function(_$controller_){
// The injector unwraps the underscores (_) from around the parameter names when matching
$controller = _$controller_;
}));

var $scope, controller;

beforeEach(function() {
$scope = {};
controller = $controller('invoiceList', { $scope: $scope });
});

describe('deleteInvoices()', function(){
it('should do nothing when no invoice is selected', function() {
$scope.invoicesToDelete = false;
var response = $scope.deleteInvoices();
expect(response).toEqual(false);
});
});
});


And the code I want to test:



$scope.deleteInvoices = function(){
if($scope.invoicesToDelete) {
return true;
} else {
return false;
}
};

Aucun commentaire:

Enregistrer un commentaire