我试图测试一个角色的指令,但我不能得到相应的模板工作.
该指令列出了这样的templateUrl
- templateUrl: 'directives/listview/view.html'
现在,当我写任何单元测试,我得到
- Error: Unexpected request: GET directives/listview/view.html
所以我必须使用$httpBackend并回应一些明智的东西
- httpBackend.whenGET('directives/listview/view.html').respond("<div>som</div>");
但是,我真的想简单地返回实际的文件,并且同步进行,所以没有等待,延迟的对象等问题.怎么做?
我现在使用
https://github.com/karma-runner/karma-ng-html2js-preprocessor.它的作用是阅读所有使用的模板,将它们转换为Angular模板,并将它们设置在$templateCache上,因此当您的应用需要它们时,它将从缓存中检索它们,而不是请求它们从服务器.
在我的karma conf文件
- files: [
- // templates
- '../**/*.html'
- ],preprocessors : {
- // generate js files from html templates
- '../**/*.html': 'ng-html2js'
- },ngHtml2JsPreprocessor: {
- // setting this option will create only a single module that contains templates
- // from all the files,so you can load them all with module('templates')
- moduleName: 'templates'
- },
然后在测试中,做好喜欢
- // Load templates
- angular.mock.module('templates');
它的工作原理