To answer my own question. Here is how to test the following controller:
var app = angular.module('mainModule');
app.controller('appController', ['$window', '$scope', '$location', '$userService', function ($window, $scope, $location, $userService) {
//body of the controller
}]);
1 In unit test create injector:
var injector = angular.injector(['ng', 'ngRoute', 'mainModule']);
2 Invoke services required:
injector.invoke( ['$userService', function ($userService) {
service = $userService;
}]);
3 Mock location service:
var mockLocation = {
path : ""
};
I just needed path
for the controller I am testing, thus I did not mock anything else, but mock whatever else you need.
4 Invoke the controller:
var scope;
var controller;
var getController = function() {
injector.invoke( ['$rootScope', '$controller', function ($rootScope, $controller) {
scope = $rootScope.$new();
controller = $controller('appController', {$scope: scope, $location: mockLocation});
}]);
};
Now the controller can be used in unit tests.
This did get rid of 'unknown provider' and other errors related to location service.
This blog post helped me with an answer, thanks to the author.