运行时grunt karma
,对其中一个指令的测试在尝试获取模板时失败.我使用ng-html2js作为预处理器.这是我的一些karma.conf.js
plugins: ['karma-chrome-launcher', 'karma-jasmine', 'ng-html2js', 'karma-ng-html2js-preprocessor'], preprocessors: { 'app/scripts/directives/**/*.html': 'ng-html2js' }, ngHtml2JsPreprocessor: { moduleName: 'templates' }
在我的测试中,我有以下内容:
'use strict'; describe('Directive: myDirective', function () { // load the directive's module beforeEach(module('myApp')); beforeEach(module('templates')); var element, scope; beforeEach(inject(function ($rootScope) { scope = $rootScope.$new(); })); it('should not show search area initially', inject(function ($compile) { element = angular.element(''); element = $compile(element)(scope); scope.$digest(); expect(element.find('.myClass').hasClass('myClass')).toBe(true); })); });
当我运行测试时,我得到了
Error: Unexpected request: GET /scripts/directives/myDirective/myDirective.html
好像预处理器没有正确地注入模板的javascript版本.
我也尝试使用模板的路径,beforeEach(module(''));
但会导致错误,内容如下:
Error: [$injector:modulerr] Failed to instantiate module...
我怎样才能解决这个问题?
我有同样的问题.确保您具有完全匹配的文件.打开Google Chrome控制台并检查文件路径是否完全相同.
在上面的例子中,我不得不在ngHtml2JsPreprocessor.stripPrefix中添加一个"/"字符串并且它有效.所以我想对于Yeoman,你应该使用
ngHtml2JsPreprocessor: { moduleName: 'templates', stripPrefix: 'app/' //add a slash }