Search code examples
angularngrx

Unable to override MockStore


I used provideMockStore for unit testing more than one selector. I need to override the MockStore for one of the test cases. Code is as follows. It is not overriding the store. I have seen documentation there it is mentioned that mockStore = TestBed.inject(MockStore). It throws error that reads "Property 'inject' doesn't exist on type 'TestBedStatic' "@angular/cli": "~8.2.1", "@ngrx/core": "^1.2.0", "@ngrx/effects": "^8.2.0", "@ngrx/entity": "^8.2.0", "@ngrx/router-store": "^8.2.0", "@ngrx/schematics": "^8.2.0", "@ngrx/store": "^8.2.0",

beforeEach(async(() => {
        TestBed.configureTestingModule({
            declarations: [
                RegisteredOwnerContainerComponent,
                RegisteredOwnerComponent,
                AddVehicleTitleComponent,
                SidePanelContentComponent,
                ExplanationComponent
            ],
            imports: [
                FlexLayoutModule,
                FormsModule,
                ReactiveFormsModule,
                MaterialModule,
                HttpClientTestingModule,
                BrowserAnimationsModule,
                LoggerModule.forRoot({ level: NgxLoggerLevel.INFO, disableConsoleLogging: false })
            ],
            providers: [
                { provide: ActivatedRoute, useValue: mockActivateRoute },
                { provide: Router, useValue: mockRouter },
                {
                    provide: EndorsementStateService,
                    useValue: {
                        navigateToNextStep: jasmine.createSpy('navigateToNextStep')
                    }
                },
                provideMockStore({
                    selectors: [
                        { selector: currentPolicySelector, value: currentPolicy },
                        { selector: currentVehicleSelector, value: currentVehicle }
                    ],
                }),
                CookieService
            ],
        }).compileComponents();
    }));

    beforeEach(async(() => {
        fixture = TestBed.createComponent(RegisteredOwnerContainerComponent);
        component = fixture.componentInstance;
        mockedStore = TestBed.get(Store);
        mockService = TestBed.get(EndorsementStateService);

        // mockedStore.select = jasmine.createSpy('select');
    }));

it('should return empty driver array if no PNI and SNI exist', fakeAsync(() => {
        mockedStore.overrideSelector(currentPolicySelector, currentPolicyWithoutPNIAndWithoutSNI);
        fixture.detectChanges();
    }));

Solution

  • TestBed.inject is an angular 9 function.

    For angular 8, use TestBed.get.