Skip to content

Instantly share code, notes, and snippets.

@sylvain-hamel
Last active August 29, 2015 13:56
Show Gist options
  • Save sylvain-hamel/9122684 to your computer and use it in GitHub Desktop.
Save sylvain-hamel/9122684 to your computer and use it in GitHub Desktop.
mockDirectiveController
// Usage :
//
// 1 - Load the real module
// ============================
// beforeEach(module('app', 'partials/my-directive.html'));
//
// 2 - Create the mock constructor function and call module(mockDirectiveController(...))
// ============================
// beforeEach(function () {
// function CtrlMock() {
// this.DoSomething = function () { };
// }
// module(mockDirectiveController('<my-directive>', 'myDirectiveCtrl', CtrlMock));
// });
//
// 3 - Instanciate directive
// ============================
// beforeEach(inject(function ($rootScope, $compile) {
// element = $compile('<my-directive/>')($rootScope);
// }));
//
// The beforeEach block where you call mockDirectiveController() must be a 'vanilla' beforeEach block;
// not one with the 'module(' or 'inject(' callback.
//
window.mockDirectiveController = function mockDirectiveController(fixture, name, controllerCtor) {
var moduleName = fixture + 'Mock';
var appMock = angular.module(moduleName, []);
if (!(controllerCtor instanceof Function)) {
throw new Error('The mock must be a constructor; not an instance of a mock');
}
appMock.controller(name, controllerCtor);
return moduleName;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment