Karma: Cannot read property * of undefined

11,176

At the top of the class, in this line, where you declare the class property initialAuthStatus:

initialAuthStatus = this.authenticationStatus.first();

this.authenticationStatus hasn't been initialized yet, gives you the error message. To make it work, put that line in ngOnInit() method and keep pure declaration part at the top of the class.

initialAuthStatus;
...
ngOnInit(){
    this.initialAuthStatus = this.authenticationStatus;
}
...
...
Share:
11,176

Related videos on Youtube

dave0688
Author by

dave0688

Lead Frontend developer / IT Architect with 11 years of professional working experience. I’ve been contributing to numerous projects so far, taking over positions of i.e. Lead Developer, Architect or PO. Usually my tasks consist of designing scalable architectures and implementing complex workflows, making them easily understandable for the users. Usually my home is the frontend, taking over the full life cycle of a project: From the first concept, prototype (code or design-only), over to the architecture design - followed by the implementation (which makes about 80% of my daily business). A close collaboration with the team is a must, whereas SCRUM has been a close companion for the last couple of years. Regarding the technology stack I mainly use Angular 9, TypeScript, Bootstrap on Frontend - and Java, Spring, Spring Boot, SQL on the backend. My experience as team lead (of international teams, up to 12 developers/project managers) helped me to lead projects of all different sizes to success.

Updated on June 04, 2022

Comments

  • dave0688
    dave0688 almost 2 years

    I have a service which I would like to test:

    @Injectable()
    export class AuthenticationService {
      initialAuthStatus = this.authenticationStatus.first();
    
      constructor(private adalService: AdalService,
                  private settingsProvider: SettingsProvider) { }
    
    
      public get authenticationStatus(): any {
        return this.adalService.userInfo.authenticationStatus;
      }
    }
    

    And a test for the service:

    describe('AuthenticationService', () => {
        let mockAdalService: any;
        let adalService: any;
        let service: any;
        let mockSettingsProvider: any;
        beforeEach(() => {
            TestBed.configureTestingModule({
              providers: [
                AuthenticationService, 
                AdalService,
                { provide: SettingsProvider, useClass: MockSettingsProvider },
                { provide: AdalService, useClass: MockAdalService }
              ]
            });
    
            mockAdalService = new MockAdalService();
            adalService = new AdalService();
            mockSettingsProvider = new MockSettingsProvider();
        });
    
        it('should be created', inject([AuthenticationService], (service: AuthenticationService) => {
            expect(service).toBeTruthy();
        }));
    });
    

    However, the test fails with the following error message:

    AuthenticationService should be created
    TypeError: Cannot read property 'authenticationStatus' of undefined
    

    It has something to do with the getter of authentication status, but I can't figure out exactly why it fails.

    Any help is greatly appreciated :)

    • Vega
      Vega about 6 years
      Error comes from this line : initialAuthStatus = this.authenticationStatus.first(); it's hasn't been initialized at that point
    • dave0688
      dave0688 about 6 years
      How can I solve it?
    • Vega
      Vega about 6 years
      put it in the constructor or ngOnInit and leave just initialAuthStatus at the top
    • dave0688
      dave0688 about 6 years
      Wow, ok I was really blind. I've put it in ngOnInit now (constructor didn't work), and it works now. Thanks a log :) Please write a post so that I can mark it as the answer.
  • dave0688
    dave0688 about 6 years
    That's the solution! However, it's this.initialAuthStatus in ngOnInit() :) Thanks for your help :)