【问题标题】:Angular 2 - Jasmine Test - Open Modal On Page LoadAngular 2 - Jasmine 测试 - 页面加载时打开模式
【发布时间】:2017-08-04 08:31:49
【问题描述】:

我有一个模式,只要用户选择页面的菜单选项,它就会打开。我遇到的问题是我不知道如何让它在我的ngOnInit中调用我的函数调用

test.component.ts

constructor(public dialog: MdDialog) { }

ngOnInit() {
    this.openTestModal();
}

openTestModal() {
    this.dialog.open(TestModalComponent, {
        disableClose: true,
        width: '600px'
    });
}

我已经导入了我的模型组件并尝试了:

change-password.component.spec.ts

import { TestModalComponent } from '../test-modal/test-modal.component';
    spyOn(component, 'openTestModal');
    spyOn(component, 'ngOnInit').and.callThrough();

    it('should be created', () => {
        expect(component).toBeTruthy();
    });

错误

没有为 TestComponent 找到组件工厂。你添加到 @NgModule.entryComponents?

但它已经在里面了

【问题讨论】:

    标签: unit-testing jasmine karma-jasmine


    【解决方案1】:

    要解决这个问题,请在测试结束时使用 entryComponents 将您的 TestComponent 添加到模块中。

    @NgModule({
      declarations: [TestComponent],
      entryComponents: [
        TestComponent,
      ],
    })
    class TestModule {}
    

    并将 TestModule 添加到 testBed 导入中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      • 2017-08-17
      • 1970-01-01
      相关资源
      最近更新 更多