I encountered an issue while testing a controller
that relies on a service
. The problem arises because the service
is currently set to null
in order to focus solely on testing the controller
.
The current test setup is failing due to the BoardService
being 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);
});
This is the addTask()
function defined in the controller:
$scope.addTask = function(category, task) {
BoardService.addTask({name : task, category : category.id}).success(function(task_id) {
// Removed code for simplicity
});
}
Lastly, here is the corresponding function within the service
:
this.addTask = function(data) {
return $http.post($rootScope.serverRoot + '/task/create', data);
}