2

我正在使用 cg-angular 来搭建我的项目。

我正在测试一个使用 templateUr 和 ng-html2js 将我的模板加载到模块中的指令。当我使用 Chrome 作为浏览器运行测试时,它没有问题,但是当我使用 Phantom 运行它时,我得到:

Error: [$injector:modulerr] Failed to instantiate module templates due to:

这是我的 Gruntfile 的相关部分:

karma:      {
     options:     {
         frameworks:           ['jasmine'],
         preprocessors:        {'directive/**/*.html':['ng-html2js']},
         files:                [  //this files data is also updated in the watch handler, if updated change there too
             '<%= dom_munger.data.appjs %>',
             'bower_components/angular-mocks/angular-mocks.js',
             'directive/**/*.html',
             createFolderGlobs(['*-spec.js'])
         ],
         ngHtml2JsPreprocessor:{
             moduleName:'templates'
         },
         logLevel:             'ERROR',
         reporters:            ['mocha'],
         autoWatch:            false, //watching is handled by grunt-contrib-watch
     },
     all_tests:   {
         browsers: ['PhantomJS', 'Chrome'],
         singleRun:false
     },
     during_watch:{
         browsers: ['PhantomJS'],
         singleRun:true
     }
 }

所以运行运行 karma:all_tests 的 grunt 测试就像一个冠军。运行 karma:during_watch 的 grunt serve 失败。

有没有办法检查 html2js 是否真的在运行?

编辑 它不是幻影。如果我在“during_watch”任务中运行 Chrome,它也会失败。

4

1 回答 1

1

弄清楚了。我正在使用 generator-cg-angular 来搭建这个项目。它使用 grunt watch 事件来设置一些选项。此事件仅在服务任务期间使用,而不是在测试或构建期间使用。我没有注意到它正在覆盖 karma.options 所以 html2js 从未被调用过。当我将它附加到我的 Gruntfile html2js 的事件部分时,它应该运行。下面是我的 Gruntfile 的相关部分,显示了更改。

if (grunt.file.exists(spec)) {
    var files = [].concat(grunt.config('dom_munger.data.appjs'));
    files.push('bower_components/angular-mocks/angular-mocks.js');
    files.push(spec);
    files.push('directive/**/*.html'); // This one
    grunt.config('karma.options.files', files);
    grunt.config('karma.options.ngHtml2JsPreprocessor.moduleName', 'templates'); // And this one
    tasksToRun.push('karma:during_watch');
}
于 2014-11-03T15:18:51.707 回答