I have been learning how to test my app written in angular stuff and I'm getting confused because it seems there are a few things against each other. I try to follow John Papa's style guide, so that I'm using Sidewaffle templates for everything basically. At the moment, it seems, the suggested template and testability against each other.
The style guide says that there is an
activate()
method in your controller which takes care about the start-up logic of the controller as you can see here. However, general testing guide line says that private method should not be tested. I have the chance to test the result of the activate() method in the provided example, because it will be passed through
vm.avengers
variable. In his Pluralsight videos he uses common.activatecontroller() method where he uses
$q.all()
to combine promises, so that you can easily call many function in controller activation phase. Let say that I have a function which won't have a result can be passed through vm, for example a post message to WebApi to authenticate the user and get the token and set it up to headers, or something like this. Here is an example (service is not injected, it is just an example):
In the controller below the only business logic is that, when the controller is instantiated the activate() method calls the func1() method which calls the OAuthenticationService.authenticate() method. From testing perspective it is important whether the service was called or not. How to test this?
Here is a very similar question where one of the answers suggests that I should use this keyword like this:
this.activate()
But a comment says that it is not working with ControllerAs syntax. I'm following the ControllerAs syntax.
When I create a mock for the service with spy on the authenticate method the test says that I was not called, I think due to that the method is private.
I run out of ideas...
Thanks for any help in advance!
Example
(function () {
'use strict';
angular
.module('app')
.controller('controller', controller);
controller.$inject = ['$location'];
function controller($location) {
/* jshint validthis:true */
var vm = this;
vm.title = 'controller';
activate();
function activate() {
func1();
}
function func1() {
OAuthAuthenticateService.authenticate(user).then(function() {
//setting up headers and other stuff, nothin will be part of $scope or vm;
});
}
}
})();
Original code:
(function () {
'use strict';
var controllerId = 'requestAuthorizationController';
angular
.module('myapp')
.controller(controllerId, requestAuthorizationController);
requestAuthorizationController.$inject = ['$rootScope',
'$scope',
'requestAuthorizationService'
'Restangular'];
function requestAuthorizationController($rootScope,
$scope,
requestAuthorizationService
Restangular) {
/* jshint validthis:true */
var vm = this;
//other business logic
activate();
function activate() {
requestAuthorization();
}
function requestAuthorization() {
vm.fired = undefined;
requestAuthorizationService.getDummy();
}
}
})();
Jasmine test:
'use strict';
describe('RequestAuthenticationController Specification', function () {
var RestangularProviderMock,
localStorageServiceProvider,
$httpProvider,
requestAuthorizationController,
requestAuthorizationServiceMock,
$rootScope;
//modules
beforeEach(function() {
angular.module('dilib.layout', []);
angular.module('http-auth-interceptor', []);
});
//providers
beforeEach(function () {
module('dilib', function(RestangularProvider, _localStorageServiceProvider_, _$httpProvider_, $provide) {
RestangularProviderMock = RestangularProvider;
localStorageServiceProvider = _localStorageServiceProvider_;
$httpProvider = _$httpProvider_;
$provide.service('requestAuthorizationService', function() {
this.getDummy = jasmine.createSpy('getDummy').and.callFake(function(num) {
});
});
});
});
//to crank up the providers
beforeEach(inject());
beforeEach(inject(function (_$rootScope_, _$controller_, _requestAuthorizationService_) {
$rootScope = _$rootScope_;
requestAuthorizationController = _$controller_;
requestAuthorizationServiceMock = _requestAuthorizationService_;
}));
describe('requestAuthorization function', function() {
it('RequestAuthorizationService.getDummy() is called', function() {
$rootScope.$digest();
expect(requestAuthorizationServiceMock.getDummy).toHaveBeenCalled();
});
});
});