茉莉花(Jasmine)使用不断变化的TemplateURL(ng-html2js-preprocessor)测试AngularJS指令

时间:2019-05-22 20:17:51

标签: javascript angularjs-directive jasmine karma-jasmine ng-html2js

我正在尝试借助ng-html2js预处理程序来为Angular指令控制器创建茉莉花单元测试,以加载模板。我面临的问题是,templateUrl的设置方式不断变化:

templateUrl: function () {
    return config.baseAddress + "templates/viewname.html?" + new Date().getTime();
}

当前,这是我在karma.conf.js中的预处理器设置:

preprocessors: {
      "../../Source/templates/*": ['ng-html2js'],
    },

    ngHtml2JsPreprocessor: {
        cacheIdFromPath: function(filepath) {
            return "http://localhost/" + filepath.match(/Templates\/.*/) + "?" + Date().getTime();

        moduleName: "templates",
    },

这不可避免地导致错误:意外请求:GET,因为测试运行时DateTime与毫秒不匹配。我什至不确定如何动态获取基址,因为在代码中将其设置为根据环境进行更改。是否可以使用正则表达式解决我的问题?我已经尝试过,但是没有成功。

0 个答案:

没有答案