加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 07:46:34 所属栏目:安全 来源:网络整理
导读:我试图测试一个角色的指令,但我不能得到相应的模板工作. 该指令列出了这样的templateUrl templateUrl: 'directives/listview/view.html' 现在,当我写任何单元测试,我得到 Error: Unexpected request: GET directives/listview/view.html 所以我必须使用$http
我试图测试一个角色的指令,但我不能得到相应的模板工作.

该指令列出了这样的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');

它的工作原理

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读