在我浏览了下面的ngrx隔离测试视频之后:John -在MockStore 8/ AngularUP中使用NgRx
我试图在我的简单项目中实现同样的功能。但我的错误是我无法理解的。有人能帮我解决吗?
这对我有很大的帮助。
测试ts文件:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { provideMockStore, MockStore } from '@ngrx/store/testing';
import { ShellHomeComponent } from './shell-home.component';
import { StoreOne } from './../../models';
import { Store, select } from '@ngrx/store';
import { cold } from 'jasmine-marbles';
describe('ShellHomeComponent', () => {
let component: ShellHomeComponent;
let fixture: ComponentFixture<ShellHomeComponent>;
let mockStore: MockStore<StoreOne>;
const loadingState = {
loading: true,
items: [{ name: '1' }]
} as StoreOne;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ShellHomeComponent ],
imports: [],
providers: [provideMockStore({initialState: loadingState})]
})
.compileComponents();
mockStore = TestBed.get(Store);
}));
beforeEach(() => {
fixture = TestBed.createComponent(ShellHomeComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should display loading as true', () => {
const expected = cold('loading', { loading: false, items: [{ name: '3' }] });
expect(component.loading).toBeObservable(expected);
});
});运行后,我将得到以下错误:
ShellHomeComponent › should display loading as true
expect(received).toEqual(expected) // deep equality
- Expected
+ Received
Array [
Object {
"frame": 0,
"notification": Notification {
- "error": undefined,
- "hasValue": true,
- "kind": "N",
- "value": true,
- },
- },
- Object {
- "frame": 10,
- "notification": Notification {
- "error": undefined,
+ "error": [TypeError: Cannot read property 'loading' of undefined],
"hasValue": false,
- "kind": "C",
+ "kind": "E",
"value": undefined,
},
},
]
41 | it('should display loading as true', () => {
42 | const expected = cold('a|', { a: true });
> 43 | expect(component.loading).toBeObservable(expected);
| ^
44 | });
45 |
46 | });
at compare (node_modules/jasmine-marbles/bundles/jasmine-marbles.umd.js:379:33)
at src/app/module1/shell/shell-home/shell-home.component.spec.ts:43:35
console.warn node_modules/@ngrx/store/bundles/store.umd.js:608
The feature name "storeOne" does not exist in the state, therefore createFeatureSelector cannot access it. Be sure it is imported in a loaded module using StoreModule.forRoot('storeOne', ...) or StoreModule.forFeature('storeOne', ...). If the default state is intended to be undefined, as is the case with router state, this development-only warning message can be ignored.
Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 6.321s发布于 2019-10-28 20:31:25
我也有过类似的问题。我的测试失败了,因为我的还原器中的state没有定义。我还在控制台中收到警告说,The feature name "my-feature" does not exist in the state's root, therefore createFeatureSelector cannot access it. Be sure it is imported in a loaded module using StoreModule.forRoot('my-feature', ...) or StoreModule.forFeature('my-feature', ...).
问题是,当我需要为整个应用程序提供模拟存储时,我正在为该功能提供模拟存储。
尝试将provideMockStore({initialState: loadingState})更改为类似于provideMockStore<State>({initialState: {shellComponent: loadingState}})的内容,其中State是应用程序全局状态的名称(确保从应用程序的state.ts文件而不是@ngrx/store导入State ),而shellComponent是要测试的特性的名称。
发布于 2020-05-27 20:29:53
在丹尼回答的基础上,你可以这样做:
providers: [
provideMockStore({
initialState: {
'addInvestigationModal': initialState
}
})
]然而,我仍然有一个错误的An error was thrown in afterAll error properties: Object({ longStack: 'TypeError: Cannot read property 'property_name' of undefined。
我修正了这一点
afterEach(() => {
fixture.destroy();
});发布于 2021-09-14 04:50:49
我收到了和@Danny一样的警告
特性名称“某些特性”不存在于状态的根中,因此createFeatureSelector无法访问它。确保它是在一个加载模块中使用StoreModule.forRoot导入的(“一些特性”,.)或者StoreModule.forFeature(“一些特征”,.)。
我忘记了必须将模块添加到导入列表中。
const someFeatureModule = StoreModule.forFeature('some-feature', someFeatureReducer);
...
@NgModule({
imports: [
someFeatureModule <-- this was missing
]
...https://stackoverflow.com/questions/58198836
复制相似问题