Currently, I am working on testing a service called documentViewer
, which relies on another service named authService
.
angular
.module('someModule')
.service('documentViewer', DocumentViewer);
/* @ngInject */
function DocumentViewer($q, authService) {
// ...
this.view = function(doc) {
//...
}
}
This is the structure of my test at the moment:
it('test', inject(function($q) {
var doc = {
view: function() {
return $q.resolve(0);
}
};
var auth = {
refreshProfileData: function() {
return $q.resolve(0);
},
};
var viewer = createViewer(auth);
}));
function createViewer(auth) {
var viewer;
module({
authService: auth
});
inject(function(documentViewer) {
viewer = documentViewer;
});
return viewer;
}
The issue arises when I attempt to use inject
to obtain a $q
, then utilize it to generate my mocks, register those mocks with module
, and finally invoke inject
again to fetch the unit I am testing.
As a result, I encounter the following error message:
Error: Injector already created, can not register a module! in bower_components/angular-mocks/angular-mocks.js (line 2278)
I have come across various responses stating that calling module
after inject
is not allowed, but none of them provide an alternative solution for a scenario like the one I am facing here.
So, what would be the correct approach in this situation?
PS: I prefer not to use beforeEach
, as I want each test to stand alone.