When it comes to unit testing Angular controllers, directly passing dependencies is not an option. This is where the lifesaver known as the $provide
service steps in!
Check out this example:
beforeEach(module('myApp', function ($provide) {
mockDependency = {
mockFunction: function() {}
};
$provide.value('dependency', mockDependency);
}));
After setting up the dependencies, you can proceed with writing your specs as usual:
beforeEach(inject(function(_MyService_, ...) {
...
MyService = _MyService_;
}));
describe("...", function() {
it("...", function() {
MyService.functionToTest();
// add your expect statements here
})
})
In the given example, note that you have the option to enclose dependencies with underscores, which are overlooked by the injector when resolving the reference name.