Unit testing 如何在AngularJS单元测试中向DOM添加div?

Unit testing 如何在AngularJS单元测试中向DOM添加div?,unit-testing,angularjs,directive,Unit Testing,Angularjs,Directive,我在指令中使用以下代码来放置工具提示 var bodyoffset = document.querySelector(".smallcontainer-content").getBoundingClientRect(); var width = elm.width(); if(bodyoffset != undefined){ var tooltipDiv = document.querySelector(".tooltip"); angular.element(tooltipD

我在指令中使用以下代码来放置工具提示

var bodyoffset = document.querySelector(".smallcontainer-content").getBoundingClientRect();
var width = elm.width();
if(bodyoffset != undefined){
    var tooltipDiv = document.querySelector(".tooltip");
    angular.element(tooltipDiv).css("top", offset.top-bodyoffset.top+"px");
    angular.element(tooltipDiv).css("left", offset.left-bodyoffset.left+width+5+"px");
}

这在单元测试中不起作用,因为类“smallcontainer”所在的div不存在。如何确保在单元测试中创建div,以便测试所有功能?

我就是这样做的:

beforeEach(inject(
    ['$compile','$rootScope', function(_$compile_) {
        var html = '<div class="smallcontainer-content"></div>';
        angular.element(document.body).append(html);
        elm = angular.element('<form name="form"><input name="password" id="passwordInput" data-ng-model="password" size="25" type="password" maxlength="20" tabindex="1" autofocus  data-my-password-check></form>');
        $compile(elm)($rootScope);
        form = $rootScope.form;

    }]
));
每次之前(注入(
['$compile','$rootScope',函数($compile){
var html='';
元素(document.body).append(html);
elm=角度元素(“”);
$compile(elm)($rootScope);
form=$rootScope.form;
}]
));
这里向文档添加html的重要部分是angular.element().append()

我在的中途测试代码示例中发现了这一点