anglejs – 使用passThrough进行单元测试的角度测试

前端之家收集整理的这篇文章主要介绍了anglejs – 使用passThrough进行单元测试的角度测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图测试一个角色的指令,但我不能得到相应的模板工作.

该指令列出了这样的templateUrl

  1. templateUrl: 'directives/listview/view.html'

现在,当我写任何单元测试,我得到

  1. Error: Unexpected request: GET directives/listview/view.html

所以我必须使用$httpBackend并回应一些明智的东西

  1. httpBackend.whenGET('directives/listview/view.html').respond("<div>som</div>");

但是,我真的想简单地返回实际的文件,并且同步进行,所以没有等待,延迟的对象等问题.怎么做?

我现在使用 https://github.com/karma-runner/karma-ng-html2js-preprocessor.它的作用是阅读所有使用的模板,将它们转换为Angular模板,并将它们设置在$templateCache上,因此当您的应用需要它们时,它将从缓存中检索它们,而不是请求它们从服务器.

在我的karma conf文件

  1. files: [
  2. // templates
  3. '../**/*.html'
  4. ],preprocessors : {
  5. // generate js files from html templates
  6. '../**/*.html': 'ng-html2js'
  7. },ngHtml2JsPreprocessor: {
  8. // setting this option will create only a single module that contains templates
  9. // from all the files,so you can load them all with module('templates')
  10. moduleName: 'templates'
  11. },

然后在测试中,做好喜欢

  1. // Load templates
  2. angular.mock.module('templates');

它的工作原理

猜你在找的Angularjs相关文章