title: TypeError: Cannot read property 'user' of null
body: <p>I am trying to pass a unit test for my function isAdmin(). The function should just return a role. It says 'user' cannot be read as I've taken a role from the information provided by the currentUser in .ts file. I'm not sure how to pass the user information in my test code(.spec.ts) file.</p>
  
  <p>TypeError: Cannot read property 'user' of null</p>
  
  <p>list-user.component.ts</p>
  
  <pre><code>constructor(public auth: AuthenticationService, public router: Router, public dialog: MatDialog) { }
  
  ngOnInit() {
      const currentUser = JSON.parse(localStorage.getItem('currentUser')).user;
      this.role = currentUser.role;
  }
  
  isAdmin() {
      return this.role.toLowerCase() === 'admin';
    }
  </code></pre>
  
  <p>list-user.component.spec.ts</p>
  
  <pre><code>import { async, ComponentFixture, TestBed } from '@angular/core/testing';
  import { ListUserComponent } from './list-user.component';
  import { FormsModule, ReactiveFormsModule } from '@angular/forms';
  import { CustomMaterialModule } from 'src/app/core/material.module';
  import { HttpClientModule } from '@angular/common/http';
  import { RouterTestingModule } from '@angular/router/testing';
  import { from } from 'rxjs';
  
  describe('ListUserComponent', () =&gt; {
    let component: ListUserComponent;
    let fixture: ComponentFixture&lt;ListUserComponent&gt;;
  
    beforeEach(async(() =&gt; {
      TestBed.configureTestingModule({
        declarations: [ ListUserComponent ],
        imports: [
          FormsModule,
          ReactiveFormsModule,
          CustomMaterialModule,
          HttpClientModule,
          RouterTestingModule
      ]
      })
      .compileComponents();
    }));
  
    beforeEach(() =&gt; {
      fixture = TestBed.createComponent(ListUserComponent);
      component = fixture.componentInstance;
      fixture.detectChanges();
    });
  
  
  
  it('should call isAdmin()', () =&gt; {
  
    let role = 'admin'
    spyOn(component, 'isAdmin').and.callFake(() =&gt; {
      return from([role]);
    });
    component.isAdmin();
    expect(component.role).toBe(role);
  });
  
  });
  </code></pre>