Angular 2 Testing - Component Instance is undefined

ぐ巨炮叔叔 提交于 2019-12-23 12:33:05

问题


I have a problem in testing in Angular 2.

describe('Spinner Component', () => {

    beforeEach(() => TestBed.configureTestingModule({
        declarations: [SpinnerComponent]
    }).compileComponents());

    beforeEach(() =>  {
        fixture = TestBed.createComponent(SpinnerComponent);
        comp = fixture.componentInstance;
        fixture.detectChanges();
    });

    it('Should Create a Spinner Component', () => {
        fixture.detectChanges();
        var compiled = fixture.debugElement.nativeElement;

        expect(compiled).toBeTruthy();
    });

    it('Should not be running', () => {
        fixture.detectChanges();
        expect(comp.isRunning).toBe(false);
    });
});

The code above shows that the Spinner Component in the test 'Should not be running'. I do not know what causes this. I get an error in the console (Please see below). I have created the component instance on the second before each as seen in the code but it states that it is undefined when ran on the second test case. I need help. I would really appreciate it. Thanks in advance.

Error in console

来源:https://stackoverflow.com/questions/40039789/angular-2-testing-component-instance-is-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!