Running unit test : Karma + Jasmine gives undefined function `controller` error - angularjs

I'm trying to implement the unit testing in existing angular project. For that I've added Grunt-Karma
karma:
unit:
options:
frameworks: ['jasmine'],
singleRun: true,
browsers: ['PhantomJS'],
files: [bower js files + project dev/test js files]
Controller is,
angular.module('app.lol.ctrls', []).controller('LOLCtrl', [
'$scope', '$filter', 'Resource', '$log', function($scope, $filter, Resource, $log) {//some logic}
And test spec is
describe('Controller: LOLCtrl', function () {
beforeEach(module('app'));
var OrderCtrl;
var scope;
var filter;
var log;
var resource=someResourceWithSomeDataFunc;
beforeEach(inject(function ($controller, $rootScope, $filter, $log) {
scope = $rootScope.$new();
OrderCtrl = $controller('LOLCtrl', {
$scope: scope,
$filter: filter,
Resource: resource,
$log: log
});
}));
it('should have lolVar to be undefined', function () {
expect(scope.lolVar).toBeUndefined();
});
});
When I run the test, I'm getting error
PhantomJS 1.9.8 (Linux 0.0.0) Controller: LOLCtrl should have lolVar to be undefined FAILED
Error: [ng:areq] Argument 'LOLCtrl' is not a function, got undefined
http://errors.angularjs.org/1.3.20/ng/areq?p0=LOLCtrl&p1=not%20a%20function%2C%20got%20undefined
undefined
at assertArg ....
I tried the solutions like using angular.mock.module instead of module in beforeEach. Also I've double checked whether I'm including the controller file.
Also the app.lol.ctrls is injected in app itself. I tried beforeEach(module(app.lol.ctrls)), but that too gives same error.
Help would be greatly appreciated.

first I think you forgot to include the dependencies of your module. without knowing the complete structure of your app-code, I guess that the code, where you defined your controller should be
angular.module('app.lol.ctrls', ['app.lol', '/*maybe other dependecies*/']).controller('LOLCtrl', [ ]);
or did you have a own module file? then it should look like this:
angular.module('app.lol.ctrls').controller('LOLCtrl', [ ]);
after resolving these dependency-problems, have a look on your index.html. Did you included
<script src="bower_components/angular-mocks/angular-mocks.js"></script>?
if not, install angular-mocks and include it.
then, as a second step, go to your test spec and add change the beforeEach parts like this:
beforeEach(function(){
module('app.lol.ctrls');
module('ngMockE2E');
});
try to run your tests again. if you're getting the same error like before, have a look at your grunt-file. the list of your files included by karma looks a little bit strange for me. maybe this could be a problem too. for help, here a snipped from my grunt-file:
karma: {
options: {
frameworks: ['jasmine'],
files: [
'<%= dom_munger.data.appjs %>',
//this files data is also updated in the watch handler, if updated change there too
'bower_components/angular-mocks/angular-mocks.js',
'application/**/*-spec.js',
'application/**/*.html',
'base/**/*-spec.js',
'error/**/*.html',
'html/**/*.html',
'*.html',
'bower_components/**/*.html'
],
preprocessors: {
'**/*.html': ['ng-html2js']
},
ngHtml2JsPreprocessor: {
moduleName: 'templates'
},
logLevel: 'WARN',
reporters: ['mocha'],
captureConsole: true,
autoWatch: false,
singleRun: true
}
}
I hope, this will help you a little bit to resolve your problem.

Related

Angular & Karma injector:modulerr - template Clip-two

I want just run karma test inside a template called Clip-two.
when i start karma conf
karma start my.conf.js
this is my error:
25 05 2017 11:36:10.911:WARN [karma]: No captured browser, open http://localhost:9876/
Chrome 49.0.2623 (Mac OS X 10.7.5) Users factory should exist FAILED
Error: [$injector:modulerr] http://errors.angularjs.org/1.4.7/$injector/modulerr?p0=clip-two&p1=Error%3A%20%5B%24injector%3Amodulerr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.4.7%2F%24injector%2Fmodulerr%3Fp0%3DngCookies%26p1%3DError%253A%2520%255B%2524injector%253Anomod%255D%2520http%253A%252F%252Ferrors.angularjs.org%252F1.4.7%252F%2524injector%252Fnomod%253Fp0%253DngCookies%250A%2520%2520%2520%2520at%2520Error%2520(native)%250A%2520%2520%2520%2520at%2520http%253A%252F%252Flocalhost%253A9876%252Fbase%252Fbower_components%252Fangular%252Fangular.min.js%253Fb6b56d0e096efc26e09d6...cc346%3A41%3A35)%0A%20%20%20%20at%20Object.workFn%20(http%3A%2F%2Flocalhost%3A9876%2Fbase%2Fbower_components%2Fangular-mocks%2Fangular-mocks.js%3Fe9f36bdb59779aa33da5eab3dfd5ffda120d58aa%3A2427%3A52)
my karma confs is made by karma init command. I added all files only
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['jasmine'],
angular: ['mocks'],
// list of files / patterns to load in the browser
files: [
'bower_components/angular/angular.min.js',
'bower_components/angular-animate/angular-animate.min.js',
'bower_components/angular-touch/angular-touch.min.js',
'bower_components/angular-sanitize/angular-sanitize.min.js',
'bower_components/angular-ui-router/release/angular-ui-router.min.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/ngstorage/ngStorage.min.js',
'bower_components/angular-translate/angular-translate.min.js',
'bower_components/angular-translate-loader-url/angular-translate-loader-url.min.js',
'bower_components/angular-translate-loader-static-files/angular-translate-loader-static-files.min.js',
'bower_components/angular-translate-storage-local/angular-translate-storage-local.min.js',
'bower_components/angular-translate-storage-cookie/angular-translate-storage-cookie.min.js',
'bower_components/oclazyload/dist/ocLazyLoad.min.js',
'bower_components/angular-breadcrumb/dist/angular-breadcrumb.min.js',
'bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js',
'bower_components/angular-loading-bar/build/loading-bar.min.js',
'bower_components/angular-scroll/angular-scroll.min.js',
'STANDARD/assets/js/app.js',
'STANDARD/assets/js/main.js',
'STANDARD/assets/js/config.constant.js',
'STANDARD/assets/js/config.router.js',
'STANDARD/assets/js/services/agente.js',
'STANDARD/assets/js/controllers/inboxCtrl.js',
'STANDARD/assets/test/*.js'
],
exclude: [
],
preprocessors: {
},
reporters: ['progress'],
port: 9876,
colors: true,
logLevel: config.LOG_WARN,
autoWatch: true,
browsers: ['Chrome'],
singleRun: false,
concurrency: Infinity
})
}
this is my unit test
describe('Users factory', function() {
// Before each test load our api.users module
beforeEach(module('clip-two'));
// Before each test set our injected Users factory (_Users_) to our local Users variable
var $controller;
beforeEach(angular.mock.inject(function(_$controller_){
$controller = _$controller_;
}));
// A simple test to verify the Users factory exists
it('should exist', function() {
expect($controller).toBeDefined();
});
});
and that my app.js
'use strict';
angular.module("clip-two", [
'ngAnimate',
'ngCookies',
'ngStorage',
'ngSanitize',
'ngTouch',
'ui.router',
'ui.bootstrap',
'oc.lazyLoad',
'cfp.loadingBar',
'ncy-angular-breadcrumb',
'duScroll',
'pascalprecht.translate',
]);
some advice?
The error says that there is a problem with ngCookies in your test.
And when I look at your karma conf file, I can't see the file containing ngcookies loaded.
You probably just need to add it in karma.conf to make it work.
Considering the error, it seems that when running karma, the host can't be reached.
Can you access your application through http://localhost:9876/?
You may try to run them again keeping your browser opened on the above address, I know it seems that it doesn't make sense, but on some systems this fixes the issue.
Then taking a look at your karma file, maybe there are some dependencies missing. Be sure that you are including all the files you need, otherwise also when your tests will run, you may have few failures.
But I can understand that now the important is that tests run :D

ngStorage breaking my application tests

I have an Angular app with a simple karma test and a very simple configuration using requirejs and Angular 1.2.28. My test is ok.
/**
* Created by jose on 7/12/2015.
*/
/*global module, inject */
define(['home', 'angularMocks','ngStorage'], function(app) {
'use strict';
describe('homeController', function () {
var scope, $location, createController;
beforeEach(module('homeApp'));
beforeEach(inject(function ($rootScope, $controller, _$location_) {
$location = _$location_;
scope = $rootScope.$new();
createController = function () {
return $controller('homeController', {
'$scope': scope
});
};
}));
it('should have message', function () {
var controller = createController();
$location.path('/');
expect($location.path()).toBe('/');
expect(scope.message).toEqual('This is Add new order screen');
});
});
});
My module:
/**
* Created by jose on 7/12/2015.
*/
'use strict';
define(['angular', 'angularRoute'], function(angular) {
var app = angular.module('homeApp', ['ngRoute']);
app.config(['$routeProvider',
function ($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'partials/home.html',
controller: 'homeController'
})
.when('/404', {
templateUrl: 'partials/404.html',
})
.otherwise({
redirectTo: '/404'
});
}
]);
app.controller('homeController', function ($scope) {
$scope.message = 'This is Add new order screen';
});
return app;
});
Unfortunately, when I try to add ngStorage as a dependency for this module, it cannot works anymore. Even try to add ngStorage to my karma configuration raise an error like this:
Error: Mismatched anonymous define() module: function (app) {
It only happens when I try to use ngStorage, when I just comment it into my karma.conf file error disappears and everything works fine...
In case of not being possible using ngStorage with Karma there are another alternatives for ngStorage? thanks
karma.conf
// Karma configuration
// Generated on Mon Jul 13 2015 09:49:28 GMT-0300 (Hora est. Sudamérica Pacífico)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine', 'requirejs'],
// list of files / patterns to load in the browser
files: [
'test-main.js',
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/angular-route/angular-route.js',
'bower_components/ngstorage/ngStorage.js',
{pattern: 'javascripts/*.js', included: false},
{pattern: 'test/**/*Spec.js', included: false}
],
// list of files to exclude
exclude: [
'javascripts/config.js'
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_DEBUG,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
Project is at github: https://github.com/jbarros35/node/tree/master/angulartemplate
kind regards.
It appears as though you are not the only one receiving errors trying to use Karma and ngStorage together. The guys at Karma have made some of their code available to help resolve the issue. You can check it out here:
https://github.com/gsklee/ngStorage/issues/117
If you look at the code of ngStorage you'll see that it calls define. So it is a proper AMD modules. Each AMD module you use in a Karma setup must be loaded with included: False in files:
{ pattern: 'bower_components/ngstorage/ngStorage.js', included: False }
Otherwise, Karma will load it with a script element, and you will get the error you got.
It's because of the async load feature of require js. Errors can be solved by this way
Go to the test-main.js.
add shim there in this way
shim:{
'yourJsModule':{
deps:['angular','yourApp']
}
}
replace "yourJsModule" with the your angular file you intend to test and the 'yourApp' with your angular module file...

Unit testing using Jasmine and Karma / AngularJS project

I am writing test cases using AngularJS with Jamsine. Working on mobile application using AngularJS and for UI using IONIC framework. So I am calling one of controller function from my spec.js for testing. Meantime, I am getting error like.
Error: Unexpected request: GET ./partials/main.html
No more request exceepted
This is my mail html file. In main.html file we are loading all other states of application.
And I already injected html in spec.js. See below code.
beforeEach(inject(function ($rootScope, $controller, $q,$injector) {
$httpBackend = $injector.get('$httpBackend');
$httpBackend.whenGET('./partials/main.html').respond(200, '');
}));
Still i ma not able to resolve error. Can i add those html files in 'spec.js' ? or any other solution ? Please help me. I am a fresher with Karma and Jasmine.
Thanks in advance.
Change your code to expectGET in place of whenGET like this
beforeEach(inject(function ($rootScope, $controller, $q,$injector) {
$httpBackend = $injector.get('$httpBackend');
$httpBackend.expectGET('./partials/main.html').respond(200);
}));
Yes Keshav. Please see below code :
Below is my karma.conf.js file. Here are all files added which I need to test. And as well as I have added a few plugins for generating reports and code coverage.
module.exports = function(config) {
config.set({
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],
plugins: [
'karma-jasmine',
'karma-coverage',
'karma-chrome-launcher',
'karma-html-reporter',
'karma-ng-html2js-preprocessor'
],
// list of files / patterns to load in the browser
files: [
'my_dir_path/www/js/libs/angular.js',
'my_dir_path/www/js/libs/angular-mocks.js',
'my_dir_path/www/js/libs/ionic.js',
'my_dir_path/www/js/libs/angular.min.js',
'my_dir_path/www/js/libs/angular-route.js',
'my_dir_path/www/js/libs/angular-sanitize.js',
'my_dir_path/www/js/libs/angular-animate.js',
'my_dir_path/www/js/libs/angular-touch.min.js',
'my_dir_path/www/js/libs/angular-ui-router.js',
'my_dir_path/www/js/libs/ionic-angular.js',
'my_dir_path/www/js/libs/http-auth-interceptor.js',
'my_dir_path/www/js/libs/ng-map.min.js',
'my_dir_path/www/js/index.js',
'my_dir_path/www/js/app.js',
'my_dir_path/www/js/controllers.js',
'my_dir_path/www/js/services.js',
'my_dir_path/www/js/startSpec.js',
'*.html'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
//'**/*.html': ['angular-templating-html2js']
"my_dir_path/www/partials/*.html": ["ng-html2js"]
},
ngHtml2JsPreprocessor: {
// If your build process changes the path to your templates,
// use stripPrefix and prependPrefix to adjust it.
//stripPrefix: "source/path/to/templates/.*/",
//prependPrefix: "web/path/to/templates/",
stripPrefix:"my_dir_path/www/",
// the name of the Angular module to create
moduleName: 'partials'
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress', 'html'],
// the default configuration
/* htmlReporter: {
outputDir: 'karma_html',
templatePath: 'node_modules/karma-html-reporter/jasmine_template.html'
}, */
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false
});
};
And this is my spec.js file. Here I am writing unit test cases for the controller. In this specs calling function "$scope.onClickResetForgot()" (That is in controller) for testing controller code. In controller I am also calling one HTTP service. After HTTP call, we are not able to back in response that why we have updating scope manually via $scope.$digest().
'use strict';
describe('loginController', function () {
var $scope,$controller,$httpBackend, AuthenticationService, def, respData = {data : {Success:true,ErrorMsg:""}};
beforeEach(inject(function ($rootScope, $controller, $q,$injector) {
$httpBackend = $injector.get('$httpBackend');
$httpBackend.expectGET('./partials/main.html').respond(200);
AuthenticationService = {
forgotPassword: function (data) {
console.log(data);
def = $q.defer();
return def.promise;
}
};
spyOn(AuthenticationService, 'forgotPassword').and.callThrough();
$scope = $rootScope.$new();
$controller = $controller('loginController', {
'$scope': $scope,
AuthenticationService : AuthenticationService
});
}));
it("Verifying login credentials, device token and device id." , function() {
$scope.Reset.Email = "harish.patidar#gmail.com";
$scope.onClickResetForgot();
def.resolve(respData);
// Update response to controller. So we need to call below line manually.
$scope.$digest();
expect(AuthenticationService.forgotPassword).toHaveBeenCalled();
$httpBackend.flush();
})
});
});
And this is my controller in controller.js.
.controller('loginController', ['$rootScope', '$scope', '$http', '$state', 'AuthenticationService', '$ionicPopup', function($rootScope, $scope, $http, $state, AuthenticationService,$ionicPopup) {
$scope.onClickResetForgot =function(type) {
console.log($scope.Reset.Email);
$scope.forgotMessage = "";
$rootScope.message = "";
AuthenticationService.forgotPassword({"Email": $scope.Reset.Email}).then(function (resp) {
var forgotPasswordPopup = $ionicPopup.alert({
title: "Forgot Password",
template: "An email has been sent to '"+$scope.Reset.Email+"' with instructions for recovering your AlertSense credentials"
});
forgotPasswordPopup.then(function(res) {
$scope.onCancelForgot();
});
});
}
}])
So after updating scope to controller, I am getting error as mentioned above. Please let me know if you need more information.

Scope variables undefined when testing controller with Karma and Jasmine with ng-scenario as framework

I've just started to discover angular unit testing mysteries using Karma & Jasmine and I have some problems with controllers tests. I've wrote tests to see if variables are defined: expect(scope.someVariable).toBeDefined(). Everything worked as expected ( every test was successfully ) untill I've added ng-scenario in karma.conf.js file.
I'm using node to run tests.
Problem: After adding ng-scenario in karma configuration file as framework all tests for checking if scope variables are defined are failing. Before adding ng-scenario all tests were successfully. Do you have any ideea why is this happening ?
Karma Conf file:
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['ng-scenario','jasmine'],
files: [ 'app/plugins/jquery/jquery.js',
'app/plugins/angular/angular.js', 'app/plugins/angular-scenario/angular-scenario.js',
'app/plugins/angular-mocks/angular-mocks.js',
'app/plugins/angular-resource/angular-resource.js',
'app/plugins/angular-cookies/angular-cookies.js',
'app/plugins/angular-sanitize/angular-sanitize.js',
'app/plugins/angular-route/angular-route.js', 'app/plugins/angular-utf8-base64/angular-utf8-base64.js',
'app/scripts/*.js',
'app/scripts/**/*.js',
'test/**/*.js',
'app/views/templates/*.html'
],
preprocessors : { 'app/views/templates/*.html': 'html2js' },
exclude: ['app/plugins/angular-scenario/angular-scenario.js'],
port: 8080,
logLevel: config.LOG_INFO,
autoWatch: false,
browsers: ['Chrome'],
singleRun: false }); };
Controller test:
'use strict'
describe('Controller: MainCtrl', function () {
beforeEach(module('qunizGameApp'));
var MainCtrl,scope, configService,feedbackService,authService,notify,resourceService;
beforeEach(inject(function ($controller, $rootScope,_configService_,_feedbackService_, _authService_,_notify_, _resourceService_) {
scope = $rootScope.$new();
MainCtrl = $controller('MainCtrl', {
$scope: scope,
configService:_configService_,
feedbackService:_feedbackService_,
authService:_authService_,
notify:_notify_,
resourceService:_resourceService_
});
configService=_configService_;
feedbackService=_feedbackService_;
authService=_authService_;
notify=_notify_;
resourceService=_resourceService_; }));
it('should be defined -configService', function () {
expect(scope.configService).toBeDefined();
});
it('should be defined - resourceService', function () {
expect(scope.resourceService).toBeDefined();
});
it('should be defined - sidebarVisible', function () {
expect(scope.sidebarVisible).toBeDefined(); });
});
Proof of working test before including ng-scenario:
Proof of failure test after including ng-scenario:
And a wierd thing is that if I debug the test in my chrome console, variables are defined:
Things read on interned and tried:
1.I've tried to define variables on scope in beforeEach section as a mock data.
2.I've tried to inject all other controller dependencies ( with undescored name as paramaters)
You can see 1 and 2 below
beforeEach(inject(function ($controller, $rootScope,_configService_,_feedbackService_, _authService_,_notify_, _resourceService_) {
scope = $rootScope.$new();
scope.configService=_configService_;
scope.authService=_authService_;
scope.resourceService=_resourceService_;
scope.sidebarVisible={};
MainCtrl = $controller('MainCtrl', {
$scope: scope,
configService:_configService_,
feedbackService:_feedbackService_,
authService:_authService_,
notify:_notify_,
resourceService:_resourceService_
});
configService=_configService_;
feedbackService=_feedbackService_;
authService=_authService_;
notify=_notify_;
resourceService=_resourceService_; }));
But scope.configService, scope.resourceService and scope.sidebarVisible are still undefined at the moment of test
I had the same problems with ng-scenario when using it with Karma. The moment I included it in karma.conf.js file, none of my tests was passing. Just don't use ng-scenario - use Protractor for integration testing. https://github.com/angular/protractor

Karma 'Unexpected Request' when testing angular directive, even with ng-html2js

After spending the last day trying to make this work, I've found that I've circled back around to the same error I was having at the beginning:
Error: Unexpected request: GET test-directive.html
I'm using Karma and Jasmine to test directives in Angular. I've looked through similar questions on StackOverflow, but find that everything that has been tried in the other examples is to no avail.
Code structure
Test-App
-src
--bower
--lib
--js
--modules
---testDir
----test.js
----test-directive.html
----test
-----test.spec.js
-test
--config
---karma.conf.js
--e2e
Karma Config
'use strict';
module.exports = function(config){
config.set({
basePath: '../../',
frameworks: ['jasmine'],
files: [
// Angular
'src/bower/angular/angular.js',
// Mocks
'src/bower/angular-mocks/angular-mocks.js',
// Libraries
'src/lib/**/*.js',
// App
'src/js/*.js',
'src/modules/*/*.js',
// Tests
'src/modules/**/test/*spec.js',
// Templates
'src/modules/**/*.html'
],
autoWatch: false,
singleRun: true,
reporters: ['progress'],
browsers: ['PhantomJS'],
preprocessors: {
'src/modules/**/*.html': 'ng-html2js'
},
ngHtml2JsPreprocessor: {
moduleName: 'dir-templates'
},
plugins: [
'karma-jasmine',
'karma-ng-html2js-preprocessor',
'karma-phantomjs-launcher',
'karma-chrome-launcher',
'karma-junit-reporter'
]
});
};
test.js
'use strict';
angular.module('modules.test', []).
directive('testDirective', [function() {
return {
restrict: 'E',
templateUrl: 'test-directive.html',
link: function($scope, $elem, $attrs) {
$scope.someFn = function() {
angular.noop();
};
}
};
}]);
test-direct.html
<span>Hello World</span>
test.spec.js
'use strict';
describe('test module', function() {
beforeEach(module('modules.test'));
/* -- DIRECTIVES------------------ */
describe('directives', function() {
var $compile, $scope, elm;
beforeEach(module('dir-templates');
beforeEach(inject(function($compile, $rootScope) {
$scope = $rootScope.$new();
elm = angular.element('<test-directive></test-directive>');
$compile(elm)($scope);
$scope.$digest();
}));
it('should have one span tag', function(){
//Jasmine test here to check for one span tag.
});
});
});
Have shortened a couple of files to stick to just where the issue is. In calling beforeEach(module('dir-templates')), it should be loading all of the matched .html files into the $templateCache and preventing the GET request that is throwing the error.
Any help would be appreciated as it's really been driving me nuts. Please comment if you have any additional questions.
So, a painstaking headache for what seems to be a two line fix. After opening Karma in Chrome (instead of PhantomJS) and looking at the source files, I noticed that when ng-html2js attaches the directive to the $templateCache it uses the entire path, not the one provided in the directive definition.
In short, 'src/modules/test/test-directive.html.js' !== 'test-directive.html.js'.
To achieve this, modify the karma.conf.js file ngHtml2JsProcessor to read like:
ngHtml2JsPreprocessor: {
stripPrefix: 'src/',
moduleName: 'dir-templates'
},
And the directive declaration's templateUrl to look like:
templateUrl: 'modules/test/test-directive.html'
To add to the comments about making sure the template name being matched on matches the prefix (has no leading slash, etc.), one other thing to check for is case.
The template cache key is case sensitive, so make sure that your directives are referencing the html files using proper casing. The template cache keys that the ngHtml2JsPreprocessor generates use the casing of the actual file name and directory names on the file system.
So if your file is named Test-Directive.html or your folder is named "Modules" but your directive is referencing "modules/test-directive.html", it won't resolve from the cache.
Case sensitivity isn't an issue with real (non-test) usage of your directive's templateurl (the GET request obviously is case insensitive and the template cache key will be generated based on whatever the initial GET request was, ie. whatever was specified in your directive).

Resources