1

I'm getting error TypeError: Cannot read property 'subscribe' of undefined on unit test, how to fix it?

part of my code:

Spec

 it('makes expected calls', () => {
        const item1: item3 = fixture.debugElement.injector.get(item3);
        spyOn(comp, 'item5');
        spyOn(item1, 'item2');
        comp.item4();
        expect(comp.item5).toHaveBeenCalled();
        expect(item1.item2).toHaveBeenCalled();
    });

if I remove this part of spec I get success.

am I using the subscribe() correct way?

1
  • Could you show the IJ in the constructor? Commented Feb 16, 2018 at 18:51

2 Answers 2

5

Try providing a mock Observable Jasmine spy returnValue for the spy created for method isPeriodCycleOpen to provide something for the component to be able subscribe to:

import { Observable } from 'rxjs/Observable';

it('makes expected calls', () => {
    const mockResponse = { cyclePeriodOpen: true, deadLineCycle: 'foobar' };

    const cycleServiceStub: CycleService = fixture.debugElement.injector.get(CycleService);
    spyOn(comp, 'setChartData');
    spyOn(cycleServiceStub, 'isPeriodCycleOpen').and.returnValue(Observable.of(mockResponse));
    comp.getCycleStatus();
    expect(comp.setChartData).toHaveBeenCalled();
    expect(cycleServiceStub.isPeriodCycleOpen).toHaveBeenCalled();
});

Hopefully that helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Hey man, thank you for help me, put one more ) after mockResponse) ... I got success =D
0

I think you are missing the return value of the spyon method, try this:

it('makes expected calls', () => {
    const cycleServiceStub: CycleService = 
    fixture.debugElement.injector.get(CycleService);
    spyOn(comp, 'setChartData');
    spyOn(cycleServiceStub, 'isPeriodCycleOpen')
        .and.callFake(() => Observable.of('yourValue')});
    comp.getCycleStatus();
    expect(comp.setChartData).toHaveBeenCalled();
            expect(cycleServiceStub.isPeriodCycleOpen).toHaveBeenCalled();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.