【发布时间】:2020-03-20 10:16:34
【问题描述】:
我是角度测试的新手,我想知道如何测试调用 ngOnInit 时调用的函数。下面是我要测试的代码
it('should call TestMethod method inside ngOnInit' , async() => {
spyOn(component , 'TestMethod')
fixture.detectChanges()
expect(component.TestMethod).toHaveBeenCalled()
})
下面是我遇到的错误
Expected spy TestMethod to have been called.
BeforeEach 在下面
beforeEach(() => {
fixture = TestBed.createComponent(CreateRoomsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
【问题讨论】:
-
TestMethod 是私有的吗?如果是这样 spyOn
(component , 'TestMethod') -
它是一个公共函数。
-
好的,你需要添加 .and.callthrough() 来绕过它。
-
喜欢这个 spyOn(component , 'TestMethod').and.callthrough() ?
标签: javascript angular unit-testing jasmine