我运行单元测试时遇到了以下错误。
以下是代码:
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();
});
});
不确定为什么会出现这个错误,尝试了所有可能的方法,并没有找到解决storeRooTmodule的多种解决方案。
添加provideMockStore和mockstore仍然会出错。
由于我是测试用例的新手,很难理解这个特定的错误。
UnitTestingModule
吗?我在想provideMockStore
需要一个带有initialState
键的对象,它不能为空。https://ngrx.io/api/store/testing/provideMockStore - AliF50