你可以使用 Chutzpah 的“模板”参考
http://matthewmanela.com/blog/chutzpah-2-4-2/ http://matthewmanela.com/blog/chutzpah-2-4-2/
/// <template path="../../MvcAngularJs1_3/ScriptsApp/directives/Templates/testTemplate.html" />
/// <reference path="../../mvcangularjs1_3/scripts/jquery-2.1.1.js" />
/// <reference path="../../mvcangularjs1_3/scripts/angular.js" />
/// <reference path="../../mvcangularjs1_3/scripts/angular-mocks.js" />
...
在参考文献下,Chutzpah 将 HTML 直接包含在测试 HTML 文件中。我为我的模板提供了一个独特的 css 类来查找它们并手动将它们放入模板 AngularJS 模板缓存中。
我的 HTML 模板
<div class="row templateTestTemplate">
<div class="col-lg-12">
<h1>Tempalte Test</h1>
</div>
</div>
该指令
angular.module("testTemplateDirective", [])
.directive("testTemplate", function() {
return {
restrict: 'A',
replace: true,
templateUrl: 'ScriptsApp/directives/Templates/testTemplate.html'
}
});
在我的单元测试中,我使用 jQuery 查找模板并将其添加到模板缓存中。 Angular 首先查看模板缓存,并且不进行 HTTP 调用。
var template = jQuery(".templateTestTemplate")[0].outerHTML;
$templateCache.put("ScriptsApp/directives/Templates/testTemplate.html", template);
到目前为止效果很好,但仅适用于 chutzpah,当您使用 Resharper 运行此测试时,测试将失败,因为 resharper 不会将模板复制到测试 HTML 中。
我还在 Chutzpah GitHub 页面上打开了一个请求,用脚本标签和 ID 包围 HTML,这样您就不需要扩展模板https://github.com/mmanela/chutzpah/issues/330 https://github.com/mmanela/chutzpah/issues/330