设置ng-htmljs-preprocessor karma预处理器

Win*_*cca 6 unit-testing angularjs karma-runner ng-html2js

我正在设置我的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)

返回""

我有吟游诗人注射设置

bard.inject(
            "$compile",
            "$controller",
            "$rootScope",
            '$templateCache',
            "haConfig",
            "$q"
        );
Run Code Online (Sandbox Code Playgroud)

这是我之前的内心

 bard.mockService(haConfig, {
            getTemplateUrl: '/tst!'
        });

        //bard.mockService(haConfig, {});
        console.log('ha config2', haConfig.getTemplateUrl());

        var html = angular.element("<div explore-hero></div>");

        console.log('htl',haConfig.getTemplateUrl());

        scope = $rootScope.$new();
        //templateCache
        element = $compile(html)(scope);
        //console.log(haConfig.getTemplateUrl(html));
        scope.$digest(element);
        console.log('missing text',haConfig.getTemplateUrl(html));

        controller = element.scope();

        console.log("element", element);
Run Code Online (Sandbox Code Playgroud)

我不知道为什么我要回一个空字符串.我正在创建html文件,但内部没有任何内容.

所有我都想知道我是否应该在我的开发工具的文件夹中显示templatesCached文件?另外,是否应该在karma.conf.js内的files数组中引用文件

现在我引用了html文件?我已经尝试过js文件,但似乎没有做任何事情

Win*_*cca 1

这个问题实际上很简单解决。我很想删除它,但如果有人遇到类似的问题,我希望它可用。

在 karma.conf.js 里面我有一个

   stripPrefix: 'rootDirectory'   // was already in place

   stripSuffix: '.js.html' // I had to make a strip on the templatesCached 

   prependSuffix: '.html'  // this is what I was searching for 
Run Code Online (Sandbox Code Playgroud)

当预处理器运行时,它模板缓存了我的所有文件。然而,它们并没有像我期待的那样结束,我无法阅读它们。我已正确设置模块和其他部件。