问题描述
我想在角度来测试指令,但我不能得到相应的模板来工作。
该指令列出了templateUrl像这样
templateUrl:指令/列表视图/ view.html
现在,当我写的单元测试,我得到
错误:意外的请求:GET指令/列表视图/ view.html
所以,我必须使用$ httpBackend和像
懂事的东西回应<$p$p><$c$c>httpBackend.whenGET('directives/listview/view.html').respond(\"<div>som</div>\");但实际上我想简单地返回实际的文件,并且还做同步,所以有与没有等待问题,延迟对象等。如何做到这一点?
我现在用的.它的作用是读取所有你使用模板,将它们转换成角模板,并将它们放在$ templateCache,所以当你的应用程序需要他们,它将从缓存中检索它们,而不是从服务器请求他们。
在我的人缘的conf文件
文件:
//模板
../**/*.html
]preprocessors:{
//生成HTML模板js文件
../**/*.html':'NG-html2js
},ngHtml2Js preprocessor:{
//设置此选项将创建一个只包含模板的单个模块
//从所有的文件,所以你可以将它们全部用模块('模板')
MODULENAME:'模板'
},
然后在测试中,不喜欢
//加载模板
angular.mock.module('模板');
和它的作品!
I'm trying to test a Directive in Angular, but I can't get the corresponding template to work.
The directive lists the templateUrl like so
templateUrl: 'directives/listview/view.html'
Now when I write any unit-test, I get
Error: Unexpected request: GET directives/listview/view.html
So I have to use the $httpBackend and respond with something sensible like
httpBackend.whenGET('directives/listview/view.html').respond("<div>som</div>");
But really I want to simply return the actual file, and also do it synchronously, so there's no issues with waits, deferred objects etc. How to do that?
I now use https://github.com/karma-runner/karma-ng-html2js-preprocessor. What it does is reading in all the templates that you use, convert them to Angular templates, and set them on the $templateCache, so when your app needs them, it will retrieve them from cache, and not request them from the server.
In my karma conf file
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'
},
And then in the test, do like
// Load templates
angular.mock.module('templates');
And it works!
这篇关于在单元测试中使用直通角测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!