如何覆盖eventEmitter对象的单元测试案例,以抽象类(Jasmine / angular8)存在
我试图在以下情况下涵盖单元测试案例,但不起作用
AbstractClass.ts
@Directive
export abstract class AbstractComponent<search = string> {
@Output()
public search: EventEmitter<string> = new EventEmitter();
}
mycomponent.component.ts.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
});
}
}
spec.ts
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();
});
I am trying to cover the unit test case for the following scenario but not working
AbstractClass.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
});
}
}
spec.ts
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();
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试以下操作:
Try this: