Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS指令模板URL需要前导斜杠,中断测试_Javascript_Angularjs_Karma Runner - Fatal编程技术网

Javascript AngularJS指令模板URL需要前导斜杠,中断测试

Javascript AngularJS指令模板URL需要前导斜杠,中断测试,javascript,angularjs,karma-runner,Javascript,Angularjs,Karma Runner,我有一个简单的指令,它只是用模板文件的内容替换一个元素。如上图所示,它坏了。Chrome中的错误是错误:意外请求:获取视图/指令/primary client.html。添加前导斜杠(即/views/directives/primary client.html)可以解决此问题 然而 在我的测试中,我完全无法让它使用前导斜杠。当我省略前导斜杠时,我加载了文件并通过了测试,但这当然破坏了实际的功能 如果需要,我可以共享更多测试代码,但是加载模板的测试示例似乎都显示了省略前导斜杠的指令。我在index

我有一个简单的指令,它只是用模板文件的内容替换一个元素。如上图所示,它坏了。Chrome中的错误是
错误:意外请求:获取视图/指令/primary client.html
。添加前导斜杠(即
/views/directives/primary client.html
)可以解决此问题

然而

在我的测试中,我完全无法让它使用前导斜杠。当我省略前导斜杠时,我加载了文件并通过了测试,但这当然破坏了实际的功能


如果需要,我可以共享更多测试代码,但是加载模板的测试示例似乎都显示了省略前导斜杠的指令。我在index.html中设置了
。是否有什么地方我做错了,导致了正斜杠?

我最近在相对路径和测试中遇到了这个问题。在我的指令中,我总是使用斜杠:

'use strict';

directives.directive('primaryClient', function() {
  return {
    restrict: 'A',
    templateUrl: 'views/directives/primary-client.html',
    scope: {
      'client': '='
    }
  };
});
这在我的应用程序中运行良好,但当我运行测试时,我得到了
意外请求:get
错误

我能够通过使用和处理相对文件路径和非相对文件路径来修复此问题,直到它正常工作

基本上,在我的karma配置中,对于预处理器,我有(注意公共前没有斜杠):

在我的karma配置中的文件列表中,我有(同样,公共前没有斜杠):

然后我在我的karma配置中设置了预处理器,如下所示(注意dist前面的斜线,这是最重要的部分)

这意味着预处理器将从
public/js/templates/my template.html
获取我的模板文件,并将其放入
/dist/js/templates/my template.html
下的缓存中

然后在我的实际测试中,我导入了特殊模块
dir模板
,如上面预处理器的moduleName下所定义的,如下所示:

ngHtml2JsPreprocessor: {
        stripPrefix: 'public',
        prependPrefix: '/dist',
        moduleName: 'dir-templates'
    }

一切正常

你能发布你的测试代码吗?您是否使用httpBackend模拟模板响应?
preprocessors: {
        'public/js/templates/**/*.html': ['ng-html2js']
    },
files: [
        'public/js/templates/**/*.html'
    ],
ngHtml2JsPreprocessor: {
        stripPrefix: 'public',
        prependPrefix: '/dist',
        moduleName: 'dir-templates'
    }
beforeEach(
    module('app')
);

beforeEach(
    module('dir-templates')
);