I'm having a problem with repeating code in my spec file where I inject a template and then compile it. To keep things DRY, I decided to extract this code into a helper function. However, I think the issue lies in trying to use multiple beforeEach
statements within the helper function. Here is the specific section of code that I am attempting to abstract:
beforeEach(module('app/views/header.html'));
beforeEach(inject(function($templateCache, $compile, $rootScope) {
template = $templateCache.get('app/views/header.html');
$templateCache.put('views/header.html', template);
var directive = angular.element('<clinical-header></clinical-header>');
element = $compile(directive)($rootScope);
$rootScope.$digest();
}));
This is the helper function I created:
var setupTemplate = function(templateName, element) {
beforeEach(module('app/views/' + templateName));
beforeEach(inject(function($templateCache, $compile, $rootScope) {
var template = $templateCache.get('app/views/' + templateName);
$templateCache.put('views/' + templateName, template);
var directive = angular.element(element);
element = $compile(directive)($rootScope);
$rootScope.$digest();
}));
And here is how I am calling the helper function:
setupTemplate('header.html', '<clinical-header></clinical-header>');
Although everything looks good at the end of my helper function, when I move to my it
block, everything becomes undefined. Is it possible to have multiple beforeEach
statements in a helper function? What is the correct way to approach this? Also, where should jasmine helper functions be placed and how can that be achieved?