在业力测试中模拟出angular.element

时间:2019-01-22 08:30:38

标签: angularjs unit-testing karma-runner

我在具有调用功能的控制器中有一个功能

var loadCtrlScope = angular.element($("#selectAllButton")).scope();

我正在尝试通过以下方式来模拟它:

var loadCtrlScope;

beforeEach(function () {    
 loadCtrlScope = angular.element('<button type="button" class="btn btn- 
 default" id="selectAllButton" ng-click="onSelectAll()">Select All</button>');            

 $compile(loadCtrlScope)(scope);

 getLoadScopeSpy = sinon.stub(angular, 'element').returns(loadCtrlScope);
}

afterEach(function(){
 getLoadScopeSpy.restore();
}

但是,当我在测试中调用该函数并在控制台中打印loadCtrlScope时,会得到响应:

  

Scope {$$ childTail:null,$$ childHead:null,$$ nextSibling:null,   $$ watchers:[Object {fn:...,last:...,get:...,exp:...,eq:...}],   $$ listeners:对象{},$$ listenerCount:对象{},$$ watchersCount:1   $ id:59,$$ ChildScope:null,$ parent:Scope {$ id:58,$$ childTail:   范围{$$ childTail:...,$$ childHead:...,$$ nextSibling:...,   $$ watchers:...,$$ listeners:...,$$ listenerCount:...,   $$ watchersCount:...,$ id:...,$$ ChildScope:...,$ parent:...,   $$ prevSibling:...,classNameForTextArea:...

此响应不包括控制器中的所有字段。

如果我尝试使用loadCtrlScope.devicesWithPSArray = []手动提供字段值,则无法使用。

0 个答案:

没有答案
相关问题