我正在尝试涵盖以下场景的单元测试用例,但无法正常工作
抽象类.ts
@Directive
export abstract class AbstractComponent<search = string> {
@Output()
public search: EventEmitter<string> = new EventEmitter();
}
MyComponent.component.ts
@Component({
selector: my-component,
templateUrl: './my.component.html',
styleUrls: ['./my.component.scss'],
})
export class MyComponent extends AbstractComponent implements OnInit {
public ngOnInit(): void {
this.search.subscribe((text: string) => { //This subscribe not covering in unit test case
// do something
});
}
}
规格
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [MyComponent],
imports: [VzUiNavigationModule, RouterTestingModule.withRoutes([]), HttpClientTestingModule],
providers: [
Store,
MyService
],
}).compileComponents();
});
it('should validate ngOnInit', () => {
spyOn(component.search, 'next').and.returnValue('ABC');
spyOn(component.onSearch, 'emit').and.returnValue(of('ABC'));
spyOn(component.onSearch, 'subscribe').and.returnValue('ABC');
component.ngOnInit();
fixture.detectChanges(true);
expect(component).toBeTruthy();
});
尝试这个:
it('should validate ngOnInit', () => {
component.ngOnInit();
component.search.emit('abc');
// what's inside of the sbuscribe should be traversed now.
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句