I am trying to test a controller
which is using a service
. However, the service
is currently null
as I want to isolate the tests in the controller
.
Here is the current test, which is not working because the BoardService
is null
beforeEach(inject(function($rootScope, $controller) {
scope = $rootScope.$new();
BoardController = $controller('BoardController', {
$scope: scope,
board: {id: 1, tasks: {}},
BoardService: null
});
}));
it ("should add a new task", function() {
var tasksBefore = scope.board.tasks.length;
scope.addTask(category, 'this is a new task');
var tasksAfter = scope.board.tasks.length;
expect(tasksAfter).toBe(tasksBefore + 1);
});
Here is the addTask()
function from the controller:
$scope.addTask = function(category, task) {
BoardService.addTask({name : task, category : category.id}).success(function(task_id) {
// Removed code for simplicity
});
}
Aaaand at last, the function in the service
:
this.addTask = function(data) {
return $http.post($rootScope.serverRoot + '/task/create', data);
}