title: How to mock Routing state in angular for unit tests
body: <p>I am writing unit tests for my component but having trouble in creating the instance of the component and showing following error:</p>
  
  <pre><code>TypeError: Cannot read property 'patientId' of null 
  </code></pre>
  
  <p>I have tried mocking all the providers including router and active router
  here is how I am calling the component:</p>
  
  <pre><code>  public onSelectedOrganizationInsurance(organizationInsurance: OrganizationInsurance) {
      this.router.navigate(['../search-insurance-plan'], {
        relativeTo: this.activatedRoute,
        state: {
          selectedOrganizationInsurance: organizationInsurance,
          patientId: this.patientId
        }
      });
  </code></pre>
  
  <p>My component.ts is</p>
  
  <pre><code>export class PatientInsurancePlanSearchComponent implements OnInit {
  
    private patientId: number = -1;
    public selectedOrganizationInsurance: OrganizationInsurance;
    public organizationInsurancePlans$: Observable&lt;OrganizationInsurancePlan[]&gt;;
  
    constructor(
      private router: Router,
      private activatedRoute: ActivatedRoute,
      private biilingHttpService: BillingHttpService
    ) {
      this.selectedOrganizationInsurance = new OrganizationInsurance();
    }
  
    ngOnInit() {
      this.patientId = history.state.patientId as number;
      this.selectedOrganizationInsurance = history.state.selectedOrganizationInsurance as OrganizationInsurance;
      this.organizationInsurancePlans$ = this.biilingHttpService.getOrganizationInsurancePlans(this.selectedOrganizationInsurance.id);
    }
  </code></pre>
  
  <p>spec.ts</p>
  
  <pre><code>class FakeInsurancePlanSearchComponent {
    @Input() public organizationInsurancePlans: OrganizationInsurancePlan[] = [];
    @Input() public selectedOrganizationInsurance: OrganizationInsurance;
    }
    beforeEach(async(() =&gt; {
      TestBed.configureTestingModule({
        declarations: [ PatientInsurancePlanSearchComponent
        , FakeInsurancePlanSearchComponent ],
        imports: [
          StoreModule.forRoot({}),
          HttpClientModule,
          RouterTestingModule,
        ],
        providers: [
          Store,
          {
            provide: ActivatedRoute, useValue: {
              state: of({ selectedorganizationInsurancePlan: 'AETNA'})
          }
        },
        BillingHttpService
        ]
      })
      .compileComponents();
    }));
  
    beforeEach(() =&gt; {
      fixture = TestBed.createComponent(PatientInsurancePlanSearchComponent);
      component = fixture.componentInstance;
      fixture.detectChanges();
    });
  
    it('should create', () =&gt; {
      expect(component).toBeTruthy();
    });
  </code></pre>
  
  <p>Kindly guide me whats I am missing.</p>