Hi there,
I am trying to write unit tests with Karma + Jasmine on a controller that uses a modal.
Controller code looks like this:
.controller('TodoCtrl', function($scope, $ionicModal) {
// The list of tasks
$scope.tasks = [];
// Creat and load the New Task modal
$ionicModal.fromTemplateUrl('templates/new-task.html', function(modal) {
$scope.taskModal = modal;
}, {
scope: $scope,
animation: 'slide-in-up'
});
$scope.addTask = function(task) {
$scope.tasks.push({
title: task.title
});
$scope.taskModal.hide();
task.title = '';
};
…
My test code is:
describe(‘Controller: TodoCtrl’, function() {
// Load the controller's module
beforeEach(module('doobiedoo'));
var TodoCtrl, scope;
// Initialise the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
TodoCtrl = $controller('TodoCtrl', {
$scope: scope
});
}));
it('should add tasks to the list', function() {
scope.task = { title: 'Task 1' };
scope.addTask(scope.task);
expect(scope.tasks.length).toBe(1);
});
The code fails with this exception:
PhantomJS 1.9.7 (Mac OS X) Controller: TodoCtrl should remove tasks from the list FAILED
TypeError: ‘undefined’ is not an object (evaluating ‘$scope.taskModal.hide’)
How is this done in general?
Thanks a lot.