我有一个指令可以在单元测试中呈现以下html:

<div id="published-envs">
    <strong>Published to</strong>
    <!-- ngRepeat: env in fundConfigurationVersion.publicationEnvironments --><div data-ng-repeat="env in fundConfigurationVersion.publicationEnvironments" class="ng-binding ng-scope">
            QA (Jan 29, 2015)
    </div><!-- end ngRepeat: env in publicationEnvironments --><div data-ng-repeat="env in publicationEnvironments" class="ng-binding ng-scope">
            PROD (Jan 30, 2015)
    </div><!-- end ngRepeat: env in publicationEnvironments -->
</div>


我试图获取<div id="published-envs">并断言其中有两个子<div>

我已经尝试了以下方法,但是它不起作用:
var environments = element.find('#published-envs'); //comes back empty

在角度单位测试中按id查找元素的正确方法是什么?

最佳答案

根据Angular Element Docs.find()


  仅限按标签名称查找


尝试

var environments = angular.element(document.getElementById('published-envs')).find('div');


JSFiddle Example

编辑

对于已编译的标记,它基本上是相同的,只是注入$compile服务并按如下方式使用

var comipledEnvironments = $compile(angular.element('<div id="published-envs"><div></div><div></div></div>'))($scope).find('div')

console.log(comipledEnvironments); // 2 <div>'s


Updated JSFiddle

10-02 14:14