测试角度指令时,isolateScope() 返回未定义 [英] isolateScope() returning undefined when testing angular directive

查看:16
本文介绍了测试角度指令时,isolateScope() 返回未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Angular v1.2.25 和 rails 资产管道,我试图测试指令的隔离范围确实已更新.由于isolateScope() 返回undefined,我期望undefined 被定义......'

Using Angular v1.2.25 and rails asset pipeline, I am attempting to test that a directive's isolate scope has indeed been updated. Since isolateScope() returns undefined I am getting expected undefined to be defined ...'

describe("cool directive", function() {

  beforeEach(module('necessaryModule'));

  var scope, $rootScope, $compile, elem,
    baseElement = '<div auto="mock_a" inc="mock_p" method="mock_m" reset-method="mock_r"></div>';

  beforeEach(inject(function( _$rootScope_, _$compile_, _$httpBackend_, $http){
    $compile = _$compile_;
    $rootScope = _$rootScope_;
    scope = $rootScope.$new();
    angular.extend(scope, {
      mock_a: [
        {name: "example1"},
        {name: "example2"}
      ],  
      mock_m: function(){
        return $http.get('/mockBackend', {
          params:{
            page:  scope.mockPage
          }   
        }); 
      },  
      mock_r: function() {
        scope.page = 1;
        scope.list = []; 
        load();
      },  
      mock_p: 1
    }); 
    $httpListGet = _$httpBackend_;
    $httpListGet.whenPOST('/api/something').respond({});
    $httpListGet.whenGET('/mockBackend').respond({name: "example3"});
    $httpListGet.whenGET('/mockBackend?page=1').respond({name: "example3"});
    $httpListGet.whenGET('/mockBackend?page=2').respond({name: "example4"});
  }));

    var create = function() {
      elem = angular.element(baseElement);
      compiledElement = $compile(elem)(scope);
      elem.scope().$apply();
      return compiledElement;
    };  

  it("has 'list' defined", function() {
    var compiledElem = create();
    var isolateElemScope = compiledElem.isolateScope();
    $rootScope.$apply();
    console.log('isolateElemScope',isolateElemScope);
    expect(isolateElemScope.list).toBeDefined();
  }); 

我希望指令范围是可访问和可测试的,但是当我测试它时却未定义.谢谢.

I'm expecting the directives scope to be accessible and testable, but I'm getting undefined when I test for it. Thank you.

推荐答案

我用下面的代码获取isolateScope

to get the isolateScope I use the following code

compiledElem.children().scope()

这是因为大多数指令不使用replace,这意味着directive标签在页面上,而directive implementation添加为该标签的子项.在这种情况下,隔离范围将属于子级.

This is because that most directives don't use replace, which means the directive tag is on the page, and the directive implementation is added as children of that tag. In that case, the isolate scope will belong to the children.

即使情况并非如此,该代码段仍应有效 - 因为子级将共享父级的范围..

Even if that is not the case, the snippet should still work - as the children will share the parent's scope..

它不起作用的唯一情况是一种极端情况,其中您有 2 个嵌套指令,其中内部指令使用替换.但我从未见过这个.

The only case it won't work, is an extreme scenario where you have 2 nested directives, where the inner one uses replace. but I never saw this.

这篇关于测试角度指令时,isolateScope() 返回未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆