如何覆盖抽象类中存在的 eventEmitter 对象的单元测试用例(jasmine / Angular8)

格尼克

我正在尝试涵盖以下场景的单元测试用例,但无法正常工作

抽象类.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();
});
阿里F50

尝试这个:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 angular 7 中使用 jasmine 和 karma 编写单元测试用例

如何为方法编写单元测试用例[Angular]

在单元测试用例中模拟Angular $ window

Angular:如何强制 EventEmitter 在测试中刷新

如何在 Angular 中为 HTTPClient get() 方法服务编写单元测试用例?

如何使用 flake8 作为单元测试用例?

如何编写单元测试用例以在 Angular 中使用下拉框列表提交表单?

Angular 4/5 单元测试用例 .toBe 在服务中

EventEmitter 在 Angular 9 中不发射对象

Angular 2 和 karma-jasmine 单元测试用例在点击链接时打开模态

Angular8服务单元测试

在Karma Jasmine单元测试用例中检查日期格式

如何从服务测试 EventEmitter

在Visual Studio中运行单元测试时出错:缺少测试用例对象

如何以 angular 覆盖 RxJS 主题的 Jasmine 单元测试

如何为CombineLatestrxjs Angular编写Jasmine Unit测试用例

在未运行测试用例的函数中创建单元测试用例类

模拟单元测试用例所需的类更改

有什么方法可以在Angular2中测试EventEmitter?

Aurelia中的Angular 2 EventEmitter

如何为BadRequest编写单元测试用例?

如何同步执行Mocah单元测试用例?

如何为Perl脚本编写单元测试用例

如何为Android API编写单元测试用例?

如何按照声明的顺序运行单元测试用例

如何为Ansible功能创建单元测试用例?

单元测试用例中的@Autowired bean为空

在VS2012中编写单元测试用例

python中super()的单元测试用例