ngrx/store - testing, throw TypeError: Cannot read property 'pipe' of undefined
ngrx/store - testing, throw TypeError: Cannot read property 'pipe' of undefined
这是我的 ts 文件:
this.store.pipe(select(subscribe.getRegCategories)).pipe(takeUntil(this.ngUnsubscribe)).subscribe(data => {
if (data && data.length) {
this.allRegCategories = data;
}
});
当我去测试时出现错误:
this.store.pipe(select(subscribe.getRegCategories)).pipe(takeUntil(this.ngUnsubscribe)).subscribe(data => {
TypeError: Cannot read property 'pipe' of undefined
这里怎么提供管道?解决这个问题的正确方法是什么?
这是我的测试规范文件:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { Store, select } from '@ngrx/store';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientModule } from '@angular/common/http';
import { ShellViewProgMgmtComponent } from './shell-view-prog-mgmt.component';
import { ViewProgMgmtComponent } from './../../components/view-prog-mgmt/view-prog-mgmt.component';
import * as actions from './../../state/actions/setup-config.actions';
describe('ShellViewProgMgmtComponent', () => {
let component: ShellViewProgMgmtComponent;
let fixture: ComponentFixture<ShellViewProgMgmtComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ShellViewProgMgmtComponent, ViewProgMgmtComponent],
imports: [HttpClientModule, RouterTestingModule],
providers: [
{
provide: Store,
useValue: {
dispatch: jest.fn(),
pipe: jest.fn()
}
}
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ShellViewProgMgmtComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
describe('ngOnInit()', () => {
it('should dispatch an event resetEditPage action in ngOnit lifecycle', () => {
const store = TestBed.get(Store);
const action = actions.resetEditPage();
const spy = jest.spyOn(store, 'dispatch');
fixture.detectChanges();
expect(spy).toHaveBeenCalledWith(action);
});
});
});
请不要嘲笑你自己的商店。
而是使用 MockStore
and/or 模拟选择器,因为它会让您的生活更轻松。
如果这是您想要的,您可以查看实现以了解如何创建模拟商店。
这是我的 ts 文件:
this.store.pipe(select(subscribe.getRegCategories)).pipe(takeUntil(this.ngUnsubscribe)).subscribe(data => {
if (data && data.length) {
this.allRegCategories = data;
}
});
当我去测试时出现错误:
this.store.pipe(select(subscribe.getRegCategories)).pipe(takeUntil(this.ngUnsubscribe)).subscribe(data => {
TypeError: Cannot read property 'pipe' of undefined
这里怎么提供管道?解决这个问题的正确方法是什么?
这是我的测试规范文件:
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { Store, select } from '@ngrx/store';
import { RouterTestingModule } from '@angular/router/testing';
import { HttpClientModule } from '@angular/common/http';
import { ShellViewProgMgmtComponent } from './shell-view-prog-mgmt.component';
import { ViewProgMgmtComponent } from './../../components/view-prog-mgmt/view-prog-mgmt.component';
import * as actions from './../../state/actions/setup-config.actions';
describe('ShellViewProgMgmtComponent', () => {
let component: ShellViewProgMgmtComponent;
let fixture: ComponentFixture<ShellViewProgMgmtComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ShellViewProgMgmtComponent, ViewProgMgmtComponent],
imports: [HttpClientModule, RouterTestingModule],
providers: [
{
provide: Store,
useValue: {
dispatch: jest.fn(),
pipe: jest.fn()
}
}
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ShellViewProgMgmtComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
describe('ngOnInit()', () => {
it('should dispatch an event resetEditPage action in ngOnit lifecycle', () => {
const store = TestBed.get(Store);
const action = actions.resetEditPage();
const spy = jest.spyOn(store, 'dispatch');
fixture.detectChanges();
expect(spy).toHaveBeenCalledWith(action);
});
});
});
请不要嘲笑你自己的商店。
而是使用 MockStore
and/or 模拟选择器,因为它会让您的生活更轻松。
如果这是您想要的,您可以查看实现以了解如何创建模拟商店。