首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在单元测试中编译嵌套指令

在单元测试中编译嵌套指令
EN

Stack Overflow用户
提问于 2015-07-07 00:04:49
回答 1查看 465关注 0票数 0

我在自定义指令的模板中有一个嵌套的自定义指令。类似于:

customDirective definition

代码语言:javascript
复制
<custom-directive></custom-directive>

customDirective.js

代码语言:javascript
复制
angular.module('example')
   .directive('customDirective', function() {
      return {
         restrict: 'E',
         replace: true,
         transclude: true,
         templateUrl: 'directives/customDirective.html'
         link: function(scope, element, attrs) {}
      };
   });

'directives/customDirective.html内部的

代码语言:javascript
复制
<div class="customDirective">
    <!-- do a bunch of stuff-->

    <!-- but wait, i have an image with a custom-fallback-src directive -->
    <img src="image.jpg" custom-fallback-src='newImage.jpg' />
</div>

customFallbackSrc.js指令

代码语言:javascript
复制
angular.module('example')
   .directive('customFallbackSrc', function() {
      return {
         restrict: 'A',
         link: function(scope, element, attrs) {

           // if image throws an error, use fallback image
           element.bind('error', function() {
              attrs.$set('src', attrs.customFallbackSrc);
           });

         }
      };
   });

在我对customDirective的单元测试中,如何正确编译指令以包含任何嵌套指令?

EN

回答 1

Stack Overflow用户

发布于 2015-07-07 03:50:48

如果你想要全面报道,我相信你最好的策略是:

  1. 为测试其核心功能的customDirective编写单元测试。但是在这个例子中它是非常稀疏的,所以这有多必要,我不确定。
  2. 为测试其核心功能的customFallbackSrc编写单元测试。根据提供的示例,您应该对两种情况进行测试:
    • 如果加载原始图像,它将不会被替换。
    • 当加载原始图像时出现错误时,回退图像将加载。

  1. 使用量角器编写集成测试,以涵盖两者之间的关系。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31257682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档