Angular 如何在角单元测试中模拟组件中的store.pipe
我是angular的新手,因为我曾尝试对一个从存储中获取数据的组件进行单元测试。我模拟了服务文件和商店。 当我尝试运行测试时,我得到如下错误。因此,我向存储添加了一些虚假数据并传递了数据。但我仍然遇到了同样的问题Angular 如何在角单元测试中模拟组件中的store.pipe,angular,unit-testing,karma-jasmine,ngrx-store,Angular,Unit Testing,Karma Jasmine,Ngrx Store,我是angular的新手,因为我曾尝试对一个从存储中获取数据的组件进行单元测试。我模拟了服务文件和商店。 当我尝试运行测试时,我得到如下错误。因此,我向存储添加了一些虚假数据并传递了数据。但我仍然遇到了同样的问题 Cannot read property 'switches' of undefined 规格文件 describe('SwitchListComponent', () => { let component: ListComponent; let fixture: Co
Cannot read property 'switches' of undefined
规格文件
describe('SwitchListComponent', () => {
let component: ListComponent;
let fixture: ComponentFixture<ListComponent>;
let store: Store<any>;
const switchData = {
switches: [
{
availability: true,
created_at: "2020-01-30T05:06:06.366Z",
description: "1",
hardware_id: "2",
id: 1018,
max_flows: 2,
name: "2",
updated_at: "2020-01-30T05:34:36.702Z"
}
],
pagination: {
currentPage: 1,
endAt: 10,
nextPage: 2,
perPage: 10,
prevPage: null,
startAt: 1,
totalCount: 1012,
totalPages: 100
}
};
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
ListComponent,
MultiSelectComponent,
],
imports: [
RouterTestingModule,
FormsModule,
StoreModule.forRoot({ switchesReducer }),
],
providers: [
{provide: SwitchesService, useClass: SwitchesServiceStub},
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ListComponent);
component = fixture.componentInstance;
store = fixture.debugElement.injector.get(Store);
store.dispatch({
type: 'FETCH_SWITCHES',
payload: {
switches: switchData.switches,
pagination: switchData.pagination
}
});
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
有谁能告诉我如何对规范中的this.store.pipe.进行模拟。ts请尝试更改以下代码
StoreModule.forRoot({ switchesReducer }),
到
有关更多信息,请参阅文档
在规范内。ts尝试更改以下代码
StoreModule.forRoot({ switchesReducer }),
到
有关更多信息,请参阅文档
StoreModule.forRoot({ switches: switchesReducer }),