I have a controller that contains a call to a service (QuestionsAttachments) and the method is .new():
angular
.module('mycontrollers',[])
.controller('DocumentsController', documentsController);
documentsController.$inject = ['$scope', 'FileUploader', 'UploadHelper', 'currentUser','QuestionsAttachments'];
function documentsController($scope, FileUploader, UploadHelper, currentUser,QuestionsAttachments){
$scope.questionsAttachments = QuestionsAttachments.new()
}
The service looks like this:
angular
.module('mycontrollers')
.factory('FileUploader', fileUploader)
.service('UploadHelper', uploadHelper)
.factory('QuestionsAttachments', lekker);
lekker.$inject = ['someservice', '$q'];
function lekker(someservice, $q) {
return {
new: function() {
return new QuestionsAttachments();
}
};
}
Notice how it has two arguments ie someservice and $q. This is where the issues start for my unit test:
describe('Customer: Controller: DocumentsController', function () {
beforeEach(function () {
module('mycontrollers');
inject(function ($injector) {
// fetch our dependencies
this.$controller = $injector.get('$controller');
this.FileUploader = $injector.get('FileUploader');
this.UploadHelper = $injector.get('UploadHelper');
this.QuestionsAttachments = $injector.get('QuestionsAttachments');
this.$scope = $injector.get('$rootScope').$new();
this.user = {
id: 'user_id',
username: 'username'
};
this.UserMock = {
currentUser: function () {
return user;
}
}
});
});
function initController(context) {
return context.$controller('DocumentsController', {
$scope: context.$scope,
FileUploader: context.FileUploader,
UploadHelper: context.UploadHelper,
currentUser: context.UserMock,
QuestionsAttachments: context.QuestionsAttachments
});
}
describe('On creation', function () {
it('should call QuestionsAttachments.new', function () {
spyOn(this.QuestionsAttachments, 'new').andReturn({/* some options */});
// instantiate the controller
initController(this);
expect(this.UploadHelper.getOptions).toHaveBeenCalled();
expect(this.$scope.uploader.onSuccessItem).toBeDefined();
});
});
});
Once initController() is called this results in this error:
Error: [$injector:unpr] Unknown provider: someserviceProvider <- someservice <- QuestionsAttachments http://ift.tt/1D1buY6
Looks like there is an issue with the 'someservice' argument in the QuestionsArguments service. How can I avoid this error or mock/stub these arguments ($q) so my test will succeed? plunkr location:http://ift.tt/1BJYEcw
Aucun commentaire:
Enregistrer un commentaire