我的指令在浏览器中运行良好。这只是我似乎无法工作的单元测试。该指令创建一个简单的滑块,并在范围上设置一些值,包括min
。
在单元测试中,$ compile(element)似乎只是将它包装在jqlite中而不用其它任何东西。好吧,它显然也给了它一个范围,但范围没有任何内容。该模板尚未应用。
我的单元测试:
describe('Given the slider directive', function () {
beforeEach(function() {
module('app');
});
beforeEach(inject(function($httpBackend){
// necessary because .whenGET().passThrough() doesn't seem to work
$httpBackend.whenGET('partials/slider.html').respond('<div class="slider horizontal">'+
'<div class="min">{{min}}</div>'+
'<div class="max">{{max}}</div>'+
'<a slider-handle class="handle" ng-class="{focus: focus}"'+
'role="slider"'+
'aria-valuemin="{{min}}" aria-valuemax="{{max}}" aria-valuenow="{{slider.value}}" aria-labelledby="{{id}}-label" aria-controls="{{id}}-value"'+
'tabindex="0"'+
'ng-keydown="handleKeyDown($event)" ng-keypress="handleKeyPress($event)"'+
'ng-focus="handleFocus($event)" ng-blur="handleBlur($event)"'+
'ng-mousedown="handleMouseDown($event)"'+
'ng-style="{\'left\': slider.left}"></a>'+
'<div ng-style="{\'left\': slider.left}" ng-show="showVals" id="{{id}}-value" class="slider-value" role="presentation">{{slider.value}}</div>'+
'</div>');
}));
it('should compile and set the scope correctly', inject(function($compile, $rootScope) {
var element = $compile('<div slider min="6" max="18" step="1" ng-model="value"></div>')($rootScope);
$rootScope.$digest();
var iScope = element.scope();
iScope.$digest();
console.log(iScope);
console.log(iScope.min);
console.log(element.html());
console.log(element);
expect(element.html()).toContain("6");
expect(element.find('div[class=min]').html()).toBe(6);
expect(iScope.min).toBe(6);
}));
});
控制台的输出是:
Scope{$id: '00T', $$childTail: null, $$childHead: null, $$prevSibling: null, $$nextSibling: null, $$watchers: null, $parent: null, $$phase: null, $root: Scope{$id: '00T', $$childTail: null, $$childHead: null, $$prevSibling: null, $$nextSibling: null, $$watchers: null, $parent: null, $$phase: null, $root: Scope{$id: ..., $$childTail: ..., $$childHead: ..., $$prevSibling: ..., $$nextSibling: ..., $$watchers: ..., $parent: ..., $$phase: ..., $root: ..., this: ..., $$destroyed: ..., $$asyncQueue: ..., $$postDigestQueue: ..., $$listeners: ..., $$isolateBindings: ..., safeApply: ...}, this: Scope{$id: ..., $$childTail: ..., $$childHead: ..., $$prevSibling: ..., $$nextSibling: ..., $$watchers: ..., $parent: ..., $$phase: ..., $root: ..., this: ..., $$destroyed: ..., $$asyncQueue: ..., $$postDigestQueue: ..., $$listeners: ..., $$isolateBindings: ..., safeApply: ...}, $$destroyed: false, $$asyncQueue: [], $$postDigestQueue: [], $$listeners: Object{}, $$isolateBindings: Object{}, safeApply: function (a) { ... }}, this: Scope{$id: '00T', $$childTail: null, $$childHead: null, $$prevSibling: null, $$nextSibling: null, $$watchers: null, $parent: null, $$phase: null, $root: Scope{$id: ..., $$childTail: ..., $$childHead: ..., $$prevSibling: ..., $$nextSibling: ..., $$watchers: ..., $parent: ..., $$phase: ..., $root: ..., this: ..., $$destroyed: ..., $$asyncQueue: ..., $$postDigestQueue: ..., $$listeners: ..., $$isolateBindings: ..., safeApply: ...}, this: Scope{$id: ..., $$childTail: ..., $$childHead: ..., $$prevSibling: ..., $$nextSibling: ..., $$watchers: ..., $parent: ..., $$phase: ..., $root: ..., this: ..., $$destroyed: ..., $$asyncQueue: ..., $$postDigestQueue: ..., $$listeners: ..., $$isolateBindings: ..., safeApply: ...}, $$destroyed: false, $$asyncQueue: [], $$postDigestQueue: [], $$listeners: Object{}, $$isolateBindings: Object{}, safeApply: function (a) { ... }}, $$destroyed: false, $$asyncQueue: [], $$postDigestQueue: [], $$listeners: Object{}, $$isolateBindings: Object{}, safeApply: function (a) { ... }}
undefined
''
{0: <div slider="" min="6" max="18" step="1" ng-model="value" class="ng-scope"></div>, length: 1}
我有一个JSFiddle with the entire (slightly cleaned up) code,但它神秘地甚至没有超过$ compile,这与我本地的问题不同。不确定JSFiddle是否有用,因为现在我有两个神秘的问题,而不是一个。
答案 0 :(得分:4)
当我处理其他事情时,几个月没有回答或评论,现在我找到了答案。这很简单:
$httpBackend.flush();
在单元测试中使用$httpBackend.whenGET()
时,总是需要刷新。因此,当您使用templateUrl测试指令时,需要在$ compile()之后刷新()。
我希望这对那里的人有用。