unit-testing - Angular 2 - Jasmine 测试 - 在页面加载时打开模态

标签 unit-testing jasmine karma-jasmine

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

中调用我的函数调用

test.component.ts

constructor(public dialog: MdDialog) { }

ngOnInit() {
    this.openTestModal();
}

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

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

更改密码.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();
    });

错误

No component factory found for TestComponent. Did you add it to @NgModule.entryComponents?

但它已经在那里了

最佳答案

要解决此问题,请在测试结束时使用 EntryComponents 将 TestComponent 添加到模块中。

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

并将 TestModule 添加到 testBed 导入中。

关于unit-testing - Angular 2 - Jasmine 测试 - 在页面加载时打开模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45501912/

相关文章:

c++ - 谷歌测试 : Parameterized tests which use an existing test fixture class?

c# - 有什么方法可以测试类似的属性吗?

javascript - Jasmine + TypeScript,找不到Spy类的withArgs()方法

javascript - 无法读取 ionic 2 单元测试中未定义的属性 '_getPortal'

angular - 失败 : Expected one matching operation for criteria "Match DocumentNode", 找不到

c# - 如何编写单元测试以确定对象是否可以被垃圾回收?

angularjs - 使用 $httpBackend 单元测试 AngularJS 给出 "Error: Unexpected Request"

node.js - 我怎样才能让 Istanbul 尔与 Node 、 Mocha 、 Jasmine 一起工作并需要 js

unit-testing - Angular 2 RC 如何对可观察的进行单元测试

unit-testing - Angular2 - 在组件中测试 ngOninit