如何覆盖eventEmitter对象的单元测试案例,以抽象类(Jasmine / angular8)存在

发布于 2025-02-08 08:13:55 字数 1422 浏览 2 评论 0原文

我试图在以下情况下涵盖单元测试案例,但不起作用

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菩提树下叶撕阳。 2025-02-15 08:13:55

尝试以下操作:

it('should validate ngOnInit', () => {
  component.ngOnInit();
  component.search.emit('abc');
  // what's inside of the sbuscribe should be traversed now.
});

Try this:

it('should validate ngOnInit', () => {
  component.ngOnInit();
  component.search.emit('abc');
  // what's inside of the sbuscribe should be traversed now.
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文