Skip to content

Instantly share code, notes, and snippets.

@cybersamx
Created August 6, 2014 18:26
Show Gist options
  • Save cybersamx/9b1994fe7eee4b969ac4 to your computer and use it in GitHub Desktop.
Save cybersamx/9b1994fe7eee4b969ac4 to your computer and use it in GitHub Desktop.
Inject a custom AngularJS directive object to a (Jasmine) unit test spec.
// Simple AngularJS directive
var directives = angular.module('myNameSpace.directives', []);
directives.directive('directive', function() {
var directive = {};
directive.restrict = 'E'; // Indicates an element directive.
directive.template = 'Hello World';
return directive;
});
// Test
describe('myNameSpace.directives', function() {
var element, scope;
beforeEach(module('myNameSpace.directives'));
beforeEach(inject(function($rootScope, $compile) {
element = angular.element(
'<directive>Old text</directive>'
);
scope = $rootScope.$new();
$compile(element)(scope);
scope.$digest();
}));
it('should display the text properly', function() {
expect(element.html()).toBe('Hello World');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment