我跑的时候遇到错误karma start:
$ karma start
INFO [karma]: Karma v0.10.2 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
WARN [preprocess]: Can not load "ng-html2js", it is not registered!
Perhaps you are missing some plugin?
...
Run Code Online (Sandbox Code Playgroud)
但是在我的包文件中,"karma-ng-html2js-preprocessor": "*",以及包含此预处理器代码的文件夹node_modules.
关于如何解决问题的任何想法?
我遇到的问题是我想做的 templateUrl: "partials/my-directive.html"
但目前我必须制作它templateUrl: "app/partials/my-directive.html以便由Karma加载.
这是我的文件夹结构(基本上是yeoman文件夹结构)
app
partials
my-directive.template.html
directives
my-directive.js
app.js
karma.conf.js
Run Code Online (Sandbox Code Playgroud)
这是指令代码
angular.module("exampleApp")
.directive("adminMod", function () {
return {
restrict: "E",
templateUrl: "app/partials/admin-mod.html",
scope: {
props: "="
}
}
});
Run Code Online (Sandbox Code Playgroud)
继承单元测试部分
beforeEach(module("app/partials/admin-mod.html"));
Run Code Online (Sandbox Code Playgroud)
继承人karma.conf.js
files: [
'app/bower_components/jquery/jquery.js',
'app/bower_components/angular/angular.js',
'app/partials/*.html'
],
preprocessors: {
'app/partials/*.html': 'ng-html2js'
},
ngHtml2JsPreprocessor: {
//prependPrefix: ???? what do I make this
},
Run Code Online (Sandbox Code Playgroud)
我想制作相对于app文件夹的url而不是我的karma.conf.file我想我已经用尽了路径的每一个组合,有人可以解释这是什么以及如何使用Yeoman角度生成器文件结构
我无法使karma-ng-html2js-preprocessor适用于外部模板.
包Json文件:
.....
"gulp-karma": "*",
"karma-coverage": "*",
"karma-jasmine": "*",
"karma-ng-html2js-preprocessor": "*",
"karma-phantomjs-launcher": "*",
.....
Run Code Online (Sandbox Code Playgroud)
Karma配置文件:
config.set({
browsers: [
....
],
frameworks: [
'jasmine'
],
plugins: [
'karma-jasmine',
'karma-phantomjs-launcher',
'karma-ng-html2js-preprocessor'
],
preprocessors: {
'app/**/*.html': 'ng-html2js'
},
ngHtml2JsPreprocessor: {
stripPrefix: 'app/'
}
});
Run Code Online (Sandbox Code Playgroud)
文件在Build文件中定义并传递给gulp-karma.以下是已定义的文件:
config = { test: {
configFile: '.../karma.conf.js',
depends: [
.......
],
files: [
"app/**/*.js",
'app/**/*.html'
]
}
}
Run Code Online (Sandbox Code Playgroud)
在我的指令规范中加载模板,如下所示:
beforeEach(module('app'));
beforeEach(module('app/tem/mytemp.html'));
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Error: [$injector:modulerr] Failed to instantiate module app/tem/mytemp.html due to:
Error: [$injector:nomod] Module 'app/tem/mytemp.html' is not available! …Run Code Online (Sandbox Code Playgroud) 我正在设置我的Karma配置文件,但我不完全理解存在的一些选项,因为我没有成功测试已经通过ngHtml2JsPreprocessor运行的模板
$templateCached
Run Code Online (Sandbox Code Playgroud)
在ngHtml2JsPreprocessor中,我可以添加一些涉及路径的键值属性.
ngHtml2JsPreprocessor: {
stripPrefix: ".*/Went all the way back to the root of my application/",
// moduleName: 'templatesCached'//
},
Run Code Online (Sandbox Code Playgroud)
我现在注释掉模板,以确保我可以访问每个文件作为模块.我正在加载模块没有错误.我可以在我的开发工具中找到templateCached版本.
beforeEach(module('template'));
Run Code Online (Sandbox Code Playgroud)
我的Templates文件夹位于我创建的基道之外.
basePath: 'Scripts/',
Run Code Online (Sandbox Code Playgroud)
我在预处理器对象中引用它
preprocessors: {
'../Templates/**/*.html' : ['ng-html2js']
},
Run Code Online (Sandbox Code Playgroud)
我的所有模板现在都是js文件和缓存.
我在package.json里面 把文件保存为
save-dev
"karma-chrome-launcher": "^0.2.2",
"karma-jasmine": "^0.2.2",
"karma-ng-html2js-preprocessor": "^0.2.1",
Run Code Online (Sandbox Code Playgroud)
我在插件中引用了我的安装.
plugins: [
'karma-chrome-launcher',
'karma-jasmine',
'karma-sinon',
'karma-ng-html2js-preprocessor'
],
Run Code Online (Sandbox Code Playgroud)
我已经加载了所有文件
files: [
//jquery libaries
// angular libraries
// Scripts files
// source app.js
// tests folder and files
]
Run Code Online (Sandbox Code Playgroud)
我的测试开始于Karma
但是,我的指令只是一个空字符串
element.html()
Run Code Online (Sandbox Code Playgroud)
返回""
我有吟游诗人注射设置 …
我用内联模板创建了一个指令,对其进行了测试,一切正常.现在我已将模板放入单独的.html文件中,并通过templateUrl指令引用它.指令适用于我的页面,但测试被打破说:
Error: [$injector:modulerr] Failed to instantiate module source/html/par
tials/template-directive-my-directive.html due to:
Error: [$injector:nomod] Module 'source/html/partials/template-directive
-my-directive.html' is not available! You either misspelled the module name
or forgot to load it. If registering a module ensure that you specify the depend
encies as the second argument.
Run Code Online (Sandbox Code Playgroud)
我使用ng-html2js预处理器没有运气(上面的错误),在我的karma配置和单元测试模块加载中尝试不同的路径和前缀.任何人都可以看到什么是错的(哪条路径,或者可能是不同的东西?)?
我的结构是:
ui/Gruntfile.js // contains the karma config
ui/source/html/index.html
ui/source/html/partials/template-directive-my-directive.html
ui/source/js/my-directive.js
ui/source/tests/unit/unit-my-directive.js
Run Code Online (Sandbox Code Playgroud)
在Gruntfile.js里面:
karma : {
unit : {
options : {
files : [
'source/lib/angular/angular.js', // angular first
'source/lib/angular/*.js', // …Run Code Online (Sandbox Code Playgroud) 继续给我这个错误:模块"模板"不可用!您要么错误拼写了模块名称,要么忘记加载它.
我在几个Angular项目中实现了Directives的单元测试,现在它似乎无法工作.这是我的karma.conf
module.exports = function(config){
config.set({
basePath : '../',
files : [
'webapp/lib/bower_components/angular/angular.js',
'webapp/lib/bower_components/angular-route/angular-route.js',
'webapp/lib/bower_components/angular-resource/angular-resource.js',
'webapp/lib/bower_components/angular-animate/angular-animate.js',
'webapp/lib/bower_components/angular-mocks/angular-mocks.js',
'webapp/lib/bower_components/jquery/jquery.js',
'webapp/lib/jasmine-jquery.js',
'webapp/js/components/**/*.html',
'webapp/app.js',
'webapp/js/**/*.js',
'test/unit/**/*.js',
{ pattern: 'webapp/stubs/*', watched: true, served: true, included: false }
],
preprocessors: {
'webapp/js/components/**/*.html': ['ng-html2js']
//'webapp/components/**/.js' : ['coverage']
},
autoWatch : true,
frameworks: ['jasmine'],
browsers : ['Chrome'],
plugins : [
'karma-chrome-launcher',
'karma-firefox-launcher',
'karma-jasmine',
'karma-ng-html2js-preprocessor'
],
junitReporter : {
outputFile: 'test_out/unit.xml',
suite: 'unit'
},
ngHtm2JsPreprocessor: {
moduleName: 'templates'
}
});
};
Run Code Online (Sandbox Code Playgroud)
我绝对100%肯定在给定位置有.html文件.我仔细检查了说明,它只是不会加载:beforeEach(module('templates'));在spec文件中.
我在这里错过了什么吗?最可能 :-)
--edit:在'webapp/js/components/**/*.html'路径中忘了'js',仍然无法正常工作.
我正在尝试使用ng-html2js来填充我的$ templateCache来运行一些单元测试.
问题是,我的模板位于不同的项目上,我使用它们的相对路径:
preprocessors: {
'..//project2//Content//Templates/**/*.html': ['ng-html2js']
},
Run Code Online (Sandbox Code Playgroud)
它将缓存条目存储为:
"C:\用户\用户\文件\ GitHub的\ solutiondir \项目2 \内容\模板\"
题:
是否有任何stripPrefix值可用于删除整个:
C:\ Users\user\Documents\GitHub\solutiondir\project2 \,无论用户名如何都可以使用?
它可能基本上是找出正确的正则表达式,因为预处理器的代码是这样的:
var log = logger.create('preprocessor.html2js');
var moduleName = config.moduleName;
var stripPrefix = new RegExp('^' + (config.stripPrefix || ''));
var prependPrefix = config.prependPrefix || '';
var stripSufix = new RegExp((config.stripSufix || '') + '$');
var cacheIdFromPath = config && config.cacheIdFromPath || function(filepath) {
return prependPrefix + filepath.replace(stripPrefix, '').replace(stripSufix, '');
};
Run Code Online (Sandbox Code Playgroud) 我的karma.conf.js包括:
plugins: [
'karma-jasmine',
'karma-phantomjs-launcher',
'karma-ng-html2js-preprocessor'
],
preprocessors: {
'../../mypath/*.html': ['ng-html2js']
},
ngHtml2JsPreprocessor: {
moduleName: 'templates'
},
Run Code Online (Sandbox Code Playgroud)
(我试过没有指定任何插件.)
我的开发依赖包括:
"karma-ng-html2js-preprocessor": "^0.2.0"`
Run Code Online (Sandbox Code Playgroud)
我的测试包括:
beforeEach(module('templates'));
Run Code Online (Sandbox Code Playgroud)
这些给出了错误:
Module 'templates' is not available!
Run Code Online (Sandbox Code Playgroud)
运行业力--log-level debug,我没有看到任何[preprocessor.html2js]条目.(我知道了Loading plugin karma-ng-html2js-preprocessor.)
我究竟做错了什么?
ng-html2js ×8
angularjs ×7
karma-runner ×5
unit-testing ×2
gulp-karma ×1
javascript ×1
regex ×1