Reputation: 779
I've looked all over SO and the internet for the last couple days and I cannot figure out what I'm missing. (Angular 1.4.7)
The app I'm working on is large, and each controller tends to have a lot of dependencies; both local services and third-party. I mock the module, and inject the dependencies. However, I've found that if I don't also mock the modules that those dependencies are registered to, I will get the "[$injector:unpr] Unknown provider:" error. To make things worse, those dependencies might have other dependencies and now I'm going down a rabbit hole of "angular.mock.module"-ing all those as well. It works if the I've managed to mock all modules.
I'm sure this isn't correct, but no blog post, existing spec files or videos mention a similar issue. I must be missing something. This happens whether I use ngMock or a helper like BardJS.
I understand the concept of mocking services, but I don't think that quite applies here. I'm not going to dive into angular ui bootstrap and start mocking everything one by one. The test below works as is. If there was another dependency in the controller, e.g. "DataService", I would have to mock its module and all its dependencies modules as well. Thanks in advance! I know it must be simple.
describe("HomeController", function() {
beforeEach(angular.mock.module("app.home"));
beforeEach(angular.mock.module("app.notify"));
beforeEach(angular.mock.module("ngDialog"));
describe("controller", function() {
var scope;
var controller;
var HomeController;
beforeEach(inject(function($rootScope, _Notify_, _ngDialog_, $controller) {
scope = $rootScope.$new();
controller = $controller;
HomeController = controller("HomeController", {
$scope: scope
});
Notify = _Notify_;
ngDialog = _ngDialog_;
}));
it('should exist', function() {
expect(HomeController).to.be.defined;
})
});
Upvotes: 0
Views: 203
Reputation: 779
Ok I figured out what I was missing.
Instead of mocking each submodule individually, I need to be mocking the entire top level app module.
So this above:
beforeEach(angular.mock.module("app.home"));
beforeEach(angular.mock.module("app.notify"));
beforeEach(angular.mock.module("ngDialog"));
becomes:
beforeEach(angular.mock.module("app"));
Thanks me!
Upvotes: 1