Skip to content

Instantly share code, notes, and snippets.

@trodrigues
Created April 6, 2015 16:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trodrigues/6cbe73943baae57e2e2a to your computer and use it in GitHub Desktop.
Save trodrigues/6cbe73943baae57e2e2a to your computer and use it in GitHub Desktop.
Mocking all sorts of stuff in angular (assumes lodash exists)
var mocks = angular.module('yourapp/mocks', []);
mocks.config(['$provide', '$controllerProvider', function ($provide, $controllerProvider) {
$provide.stubDirective = function (name, definition) {
$provide.factory(name + 'Directive', function () {
return [_.extend({
name: name,
restrict: 'A',
priority: 0,
}, definition)];
});
};
$provide.removeDirectives = function () {
_.flatten(arguments).forEach(function (directive) {
var fullName = directive + 'Directive';
$provide.factory(fullName, function () {
return [];
});
});
};
$provide.removeController = function (label, fakeController) {
$controllerProvider.register(label, fakeController || angular.noop);
};
$provide.removeControllers = function () {
_.flatten(arguments).forEach(function (controller) {
$controllerProvider.register(controller, angular.noop);
});
};
$provide.stubFilter = function (filterName, returnValue) {
$provide.value(filterName+'Filter', function () {return returnValue || '';});
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment