Last active
August 29, 2015 13:56
-
-
Save sylvain-hamel/9122684 to your computer and use it in GitHub Desktop.
mockDirectiveController
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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