title: Mock Observable service on ngOnInit
body: <p>PS This is first time I will be writing test cases.</p>
  
  <p>Env details:
   - Angular 4.4.3</p>
  
  <p><strong>in component</strong></p>
  
  <pre><code>currentToken: string;
  
  constructor(private tokenService: TokenService){}
  
  ngOnInit(): void {
     this.tokenService.getCurrentToken().subscribe((token: string) =&gt; {
        this.currentToken = token;
     });
  }
  </code></pre>
  
  <p><strong>in TokenService</strong></p>
  
  <pre><code>export class TokenService {
     constructor(private router: Router) { }
  
     getCurrentToken(): Observable&lt;string&gt; {
        const token: string = sessionStorage.getItem("token");
        if (token) {
           return Observable.of(token);
        }else {
           this.router.navigate(['home']);
        }
     }
  }
  </code></pre>
  
  <p><strong>In spec.ts</strong></p>
  
  <pre><code>describe("AccountComponent" , () =&gt; {
     let component: AccountComponent;
     let fixture: ComponentFixture&lt;AccountComponent&gt;;
  
     beforeEach(async(() =&gt; {
        TestBed.configureTestingModule({
          imports: [],
          declaration: [ TokenService ]
        }).compileComponents();
     }));
  
     beforeEach(() =&gt; {
       fixture = TestBed.createComponent(AccountComponent);
       component = fixture.createInstance;
       fixture.detectChanges();
       component.ngOnInit();
     });
  
     it("should create", () =&gt; {
       expect(component).toBeTruthy();
     });
  });
  </code></pre>
  
  <p>This is simple test which will come when i used angular cli. As you may be already know that I am getting error like below.</p>
  
  <p><em>"Cannot read property 'subscribe' of undefined"</em></p>
  
  <p>This is common kind of method which i will writing in all new component.</p>
  
  <p>TIA</p>