【问题标题】:Use angularjs directive templates from $templateCache in karma tests在业力测试中使用来自 $templateCache 的 angularjs 指令模板
【发布时间】:2015-03-22 16:28:05
【问题描述】:

我知道如何load templates in my karma tests using the ng-html2js preprocessor。然而,在我们的项目中,我们已经有一个 templates.js 文件,看起来像这样:

angular.module("myApp").run(
    ["$templateCache", function($templateCache) {
        $templateCache.put(
            "app/directive-template.html",
            "<span>Some fancy template.</span>");
    ...
});

它是使用gulp-angular-templatecache 插件生成的。这对生产非常有用,但对测试失败。该文件包含在 karma.conf.js 中并正确加载,但运行函数仅在执行测试后执行。

如何让 karma 等待运行阶段,以便在执行测试之前加载模板?

【问题讨论】:

    标签: angularjs templates karma-runner


    【解决方案1】:

    如果你在每个 myApp 模块之前,那么你的模板将被加载

    我使用 gulp ng-hmtl2js 创建我的 template.js 文件 条目如下所示:

        //app template
    gulp.task('template_app', function () {
        return gulp.src('./app/**/*.tpl.html')
            .pipe(ngHtml2Js({
                moduleName: "template.app"
            }))
            .pipe(concat('app.template.js'))
            .pipe(gulp.dest('./app'));
    });
    

    然后在我的测试中:

    beforeEach(module('template.app'));

    查看对我有帮助的答案@https://stackoverflow.com/a/28832167/1843436

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-09
      • 2015-11-15
      • 2017-05-26
      • 1970-01-01
      • 1970-01-01
      • 2017-01-24
      • 2016-06-19
      • 1970-01-01
      相关资源
      最近更新 更多