vendredi 23 janvier 2015

Testing controlers with services

I'm new to angular and new to unit-testing, which is an terrible situation. Since days I hang on the same problem. Walking trough the web an SO was not helpful.


I have an application module MyApp including basic things an services, that are needed in all other modules, like service for logging loggingService


I also have an module for handling everything about map&geo-positon, called MapModule and I have an main module for application logic, called MainModule


The MainModule contains a controller, that I like to test: messageSendCtrl The controller has some dependencies, like services from MapModule.


And: MainModule and MapModule has dependencies to the MyApp, because the loggingServiceis needed everywhere.


The code looks like that (pseudo-code):


MyApp



var MyApp = angular
.module('MyApp', ['ngRoute','MainModule','MapModule']);

MyApp.service('loggingService', function (one, two) {
[..] /*logging data somewhere for debugging application*/
});


MainModule



var MainModule = angular
.module('MainModule', []);
MainModule.controller('messageSendCtrl',
function($scope,$http, $location, locationService, loggingService) {
[...]
});


MapModule



var MapModule = angular
.module('MapModule', ['uiGmapgoogle-maps']);
MapModule.service('locationService', function (loggingService) {
[...]


What I like to test is the messageSendCtrl from the MainModule. (probably) I was able to inject the location service into the test environment. But injecting the locationService was not successful.


Probably because locationService also uses the loggingService.


Running the test results in



Error: [$injector:unpr] Unknown provider: loggingServiceProvider <- loggingService <- locationService


My test looks like that:



describe('saving a document', function() {

beforeEach(module('MainModule'));
beforeEach(module('MyApp'));
beforeEach(module('MapModule'));

describe ('messageSendCtrl', function () {
var scope,ctrl,locationService,loggingService;

beforeEach(inject(function($rootScope, $controller,_locationService_,_loggingService_) {
scope = $rootScope.$new();
ctrl = $controller('messageSendCtrl',
{$scope: scope,
locationService: _locationService_,
loggingService : _loggingService_ });
}));


it('should actual not saved', function(){
expect(scope.result).to.equal('unsaved');
});
})
});


So who can I solve the dependencies? Or is there an design problem at my application?


Aucun commentaire:

Enregistrer un commentaire