【发布时间】:2020-08-05 03:04:04
【问题描述】:
我有一个指令可以在鼠标悬停期间更改元素的背景颜色,如下所示。
import {Directive, ElementRef, HostListener, Input} from '@angular/core';
@Directive({
selector: '[appHighlightme]'
})
export class HighlightmeDirective {
@Input('appHighlightme') color : string
constructor(private el:ElementRef) { }
@HostListener('mouseenter') onMouseEnter(){
this.highlight(this.color || 'yellow');
}
@HostListener('mouseleave') onMouseLeave(){
this.highlight('inherit');
}
highlight(color){
this.el.nativeElement.style.backgroundColor = color;
}
}
我试图为这个指令编写一个单元测试用例,如下所示
import { HighlightmeDirective } from './highlightme.directive';
import {ChangeDetectionStrategy, Component, DebugElement, ViewChild} from '@angular/core';
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
@Component({
selector: 'my-test-component',
template: '<a [appHighlightme]="color" >test</a>'
})
export class TestComponent {
color:string = 'blue';
}
describe('HighlightmeDirective', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
let inputEl: DebugElement;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
TestComponent,
HighlightmeDirective
]
})
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
inputEl = fixture.debugElement.query(By.directive(HighlightmeDirective));
});
it('detect hover changes', () => {
inputEl.triggerEventHandler('mouseenter', {});
fixture.detectChanges();
expect(inputEl.nativeElement.style.backgroundColor).toBe(component.color);
inputEl.triggerEventHandler('mouseleave', {});
fixture.detectChanges();
expect(inputEl.nativeElement.style.backgroundColor).toBe('inherit');
});
it('should create an instance', () => {
const directive = new HighlightmeDirective(inputEl);
expect(directive).toBeTruthy();
});
});
该指令在其他组件中运行良好。它接受 ts 文件中定义的颜色参数变量,并在悬停元素时将其用作 bg 颜色。但是,当我尝试对其进行单元测试时,指令未检测到从 TestComponent 传递的颜色参数,并且测试用例失败并显示以下错误消息。
错误:预期“黄色”为“蓝色”。 - 黄色被设置为默认悬停颜色
【问题讨论】:
-
只是一个猜测......也许需要更改检测周期,以便组件中的颜色正确。我不确定这是否有意义,但您的测试代码显然是正确的。您可以尝试两件事:(1)使您的测试异步(fakeAsync)并尝试让时间过去; (2)复制第一个
fixture.detectChanges()。 -
在上面的评论中有一个错字:“它需要两个变更检测周期”...
-
我尝试过 fakeAsync。但它不起作用('检测悬停变化',fakeAsync(()=> { inputEl.triggerEventHandler('mouseenter',{});fixture.detectChanges();滴答(2000);期望(inputEl.nativeElement.style .backgroundColor).toBe(component.color); inputEl.triggerEventHandler('mouseleave', {}); fixture.detectChanges(); expect(inputEl.nativeElement.style.backgroundColor).toBe('inherit'); })) ;
-
复制 fixture.detectChanges() 也不起作用
标签: angular unit-testing karma-jasmine angular2-directives mouseenter