Hi,
I struggle with unit tests. I tried to search for a solution in google, but
without success.
So I figured I would post here, to maybe get some insight from you guys.
My controller looks like this:
angular.module('screensSection').controller('TimelineFormController',
function($scope, $injector, $routeParams,
$translate) {
// TimelineFormController inherits SessionFormController, and injects
TimelineService as SessionService
$injector.invoke(['$scope', 'TimelineService', '$translate',
'$routeParams', '$location', 'GlobalLoading',
SessionFormController], this, {$scope: $scope});
this.setTitle = function() {
if ($routeParams.sessionId) {
$translate('EDIT_TIMELINE').then(function(translated) {
$scope.title = translated;
});
} else {
$translate('CREATE_TIMELINE').then(function(translated) {
$scope.title = translated;
});
}
};
this.setTitle();
});
I'm trying to check that $scope.title has been set with the correct title.
So I wrote the following test:
'use strict';
describe('TimelineFormController tests', function() {
beforeEach(module('screensSection'));
var controller,
$scope,
translateMock;
beforeEach(inject(function($rootScope, $controller, $q) {
$scope = $rootScope.$new();
translateMock = function(wording) {
console.log('translateMock called!');
var deferred = $q.defer(),
title;
switch (wording) {
case 'EDIT_TIMELINE':
title = 'Edit timeline'
break;
case 'CREATE_TIMELINE':
title = 'Create timeline';
break;
default:
title = 'Default';
}
deferred.resolve(title);
return deferred.promise;
};
console.log('before controller creation');
controller = $controller('TimelineFormController', {
$scope: $scope,
$translate: translateMock
});
}));
it('should set timeline creation title', function() {
controller.setTitle();
expect($scope.title).toBe('Create timeline');
});
});
So I think it fails because there is no "then()" method on my mock. Do you
agree?
I don't know how else I could achieve my test?
Can anyone help, or give any advice?
Thanks in advance!
--
Benjamin
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.