NullInjectorError: No provider for StoreRootModule



在运行单元测试用例时出现以下错误

NullInjectorError: R3InjectorError(DynamicTestModule)[StoreFeatureModule - > StoreRootModule - > StoreRootModule]:
NullInjectorError: No provider
for StoreRootModule!
error properties: Object({
ngTempTokenPath: null,
ngTokenPath: ['StoreFeatureModule', 'StoreRootModule', 'StoreRootModule']
})
NullInjectorError: R3InjectorError(DynamicTestModule)[StoreFeatureModule - > StoreRootModule - > StoreRootModule]:
NullInjectorError: No provider
for StoreRootModule!

下面是代码:

import {
ComponentFixture,
TestBed,
fakeAsync,
tick,
} from '@angular/core/testing';
import {
MenuComponent
} from './menu.component';
import {
RouterTestingModule
} from '@angular/router/testing';
import {
HttpClientTestingModule
} from '@angular/common/http/testing';
import {
UnitTestingModule
} from 'src/app/shared/unit-testing/unit-testing.module';
import {
Location
} from '@angular/common';
import {
MockStore
} from '@ngrx/store/testing';
import {
AppState
} from 'src/app/state';
import {
provideMockStore
} from '@ngrx/store/testing';
import {
CUSTOM_ELEMENTS_SCHEMA
} from '@angular/core';

describe('MenuComponent', (): void => {
let component: MenuComponent;
let fixture: ComponentFixture < MenuComponent > ;
let location: Location;
let store: MockStore < AppState > ;
beforeEach(async(): Promise < void > => {
await TestBed.configureTestingModule({
schemas: [CUSTOM_ELEMENTS_SCHEMA],
imports: [RouterTestingModule, HttpClientTestingModule, UnitTestingModule],
declarations: [MenuComponent],
providers: [provideMockStore({})],
})
.compileComponents();
location = TestBed.get(Location);
store = TestBed.inject(MockStore);
});
beforeEach((): void => {
fixture = TestBed.createComponent(MenuComponent);
component = fixture.componentInstance;
fixture.detectChanges();
spyOn(store, 'dispatch').and.callFake(() => {});
});
it('should create', (): void => {
expect(component).toBeTruthy();
});

});

不知道为什么会出现这个错误,尝试了所有可能的方法,但没有找到很多解决方案。

添加了provideMockStore和mockstore,仍然给出错误

由于我不熟悉测试用例,所以很难理解这个特定的错误。

尝试添加到您的:

供应商:

providers: [
MockProvider(Store)
]

:

imports: [
StoreModule.forRoot({})
]

在providers中添加一个空对象

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [PuntersClubJoinClubPageComponent],
imports: [HttpClientTestingModule],
providers: [provideMockStore({})],
}).compileComponents();

}));

最新更新