在单元测试中使用passThrough进行角度测试

Wil*_*der 15 directive angularjs angularjs-directive

我正试图在Angular中测试一个指令,但是我无法使用相应的模板.

该指令列出了templateUrl,如下所示

templateUrl: 'directives/listview/view.html'
Run Code Online (Sandbox Code Playgroud)

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

Error: Unexpected request: GET directives/listview/view.html
Run Code Online (Sandbox Code Playgroud)

所以我必须使用$ httpBackend并回复一些明智的事情

httpBackend.whenGET('directives/listview/view.html').respond("<div>som</div>");
Run Code Online (Sandbox Code Playgroud)

但实际上我想简单地返回实际文件,并同步执行,因此等待,延迟对象等没有问题.如何做到这一点?

Wil*_*der 13

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

在我的业力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'
},
Run Code Online (Sandbox Code Playgroud)

然后在测试中,喜欢

// Load templates
angular.mock.module('templates');
Run Code Online (Sandbox Code Playgroud)

它的工作原理!

  • 可能你需要设置像cacheIdFromPath:function(filepath){return filepath.substr(filepath.indexOf("appname")+ 8); },在ngHtml2JsPreprocessor部分.因为它缓存模板的路径默认是相对于*disk*,而我们想要的是相对于我们的应用程序根目录.另请参阅http://willemmulder.tumblr.com/post/63827986070/unit-testing-angular-modules-and-directives-with以获得更详细的说明 (3认同)
  • 我需要在`ngHtml2JsPreprocessor` karma配置中添加`stripPrefix:'app /'`. (3认同)
  • 嗨,您好.我在加载模板时遇到了一些问题.我按照非常清楚的说明进行操作,但测试仍在尝试加载模板.如果你有时间检查https://gist.github.com/andreareginato/7168181这里的代码 (2认同)
  • 你的建议是对的.这是一个路径问题.我用这种方式解决了它https://github.com/angular/angular.js/issues/2512#issuecomment-27146056 (2认同)

Ben*_*esh 10

确保在beforeEach中包含ngMockE2E模块

如果不这样做,$ browser服务器模拟将在whenGET调用时不被实例化,并且返回值将不会设置该passThrough函数

beforeEach(function() {
   module('yourModule');
   module('ngMockE2E'); //<-- IMPORTANT!

   inject(function(_$httpBackend_) {
    $httpBackend = _$httpBackend_;
    $httpBackend.whenGET('somefile.html').passThrough();
   });
});
Run Code Online (Sandbox Code Playgroud)

在angular-mocks.js中设置它的位置:

有问题的源代码是$ httpBackend mock的when函数:

function (method, url, data, headers) {
  var definition = new MockHttpExpectation(method, url, data, headers),
      chain = {
        respond: function(status, data, headers) {
          definition.response = createResponse(status, data, headers);
        }
      };

  if ($browser) {
    chain.passThrough = function() {
      definition.passThrough = true;
    };
  }
  definitions.push(definition);
  return chain;
} 
Run Code Online (Sandbox Code Playgroud)

  • 谢谢.我尝试过,但请求似乎没有得到满足.我得到"错误:意外请求:GET指令/ listview/view.html.不再需要请求",而我设置的规则是httpBackend.whenGET("directives/listview/view.html").passThrough(); 应该工作,对吗? (17认同)
  • 不幸的是,这只适用于e2e测试,而不适用于单元测试. (3认同)
  • 在karma配置中,我包括angular,angular-mock和我的项目文件.然后在测试中,我可以使用$ httpBackend,但是当我尝试使用.passThrough()时,我得到TypeError:'undefined'不是一个评估'httpBackend.whenGET(/.*/).passThrough()')的函数.您是否包含特殊依赖项以使其有效? (2认同)