我的指令在浏览器中运行良好。这只是我似乎无法工作的单元测试。该指令创建一个简单的滑块,并在范围上设置一些值,包括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,但它神秘地甚至没有通过 $compile,这与我在本地遇到的问题不同。不确定 JSFiddle 是否有用,因为现在我有两个神秘的问题而不是一个。