如何设置新状态来测试可观察对象?
How do I set a new State to test an observable?
我整天都在尝试在 jasmine 中做一个简单的测试,但我认为我做错了什么。我有一段代码想测试,但我不能进去。我试图遵循 nrgx 7 文档,但我失败了。
下面的单元测试应该测试我的 enderecoFeatureSubscription。 store.setState({ cep: null, endereco: RES }) 对商店没有做任何事情,所以我的订阅没有做任何事情
let component: FormComponent;
let fixture: ComponentFixture<FormComponent>;
let store: MockStore<ICepState>
const initialState = {
cep: null, endereco: null
};
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [FormComponent],
imports: [StoreModule.forRoot({}),],
providers: [
provideMockStore({ initialState: CEPSTATE })
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(FormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
store = TestBed.get(Store);
});
it('should test enderecoFeatureSubscription ', () => {
store.setState({ cep: null, endereco: RES })
expect(component.endereco).toEqual(RES)
});
组件
private enderecoFeatureSubscription = this.store.pipe(select(enderecoFeatureSelector)).subscribe((endereco: IEndereco | any) => {
if (!endereco) {
return;
}
this.endereco = endereco
})
如果你能帮助我谢谢你,因为我已经浪费了很多时间。
在 ngrx 版本中。 > 8.0.0,如果您在各自覆盖的选择器上使用 store.setState
,则有一种方法 store.refreshState
可以刷新状态。不幸的是,refreshState
方法在 ngrx 7 中不存在。有一个替代方法 - 您应该使用 store.overrideSelector
覆盖所需的选择器,就像这样 -
it('should test enderecoFeatureSubscription ', () => {
store.overrideSelector(enderecoFeatureSelector, <put you mocked value>
fixture.detectChanges(); //MAKE sure to remove fixture.detectChanges() from beforeEach
expect(component.endereco).toEqual(RES)
});
我对我的测试工作做了一些修改。
1 - 删除 'const initialState' 并从我的应用程序状态文件中导入。
2 - MockStore 的类型,我更改为我的应用程序状态类型
3 - 在测试中,我将新值设置为 'cepState.endereco' 并使用 initialState 调用 setState
4 - 我将 'store' 更改为 'mockStore',但这并没有什么区别
5 - 最后,我带来了正确的进口
看下面的代码:
describe('FormComponent', () => {
let component: FormComponent;
let fixture: ComponentFixture<FormComponent>;
let mockStore: MockStore<AppState>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [FormComponent],
imports: [
StoreModule.forRoot({ 'cepState': CepReducer })
],
providers: [provideMockStore({ initialState })]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(FormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
mockStore = TestBed.get(Store);
});
it('should test new endereco state', () => {
initialState.cepState.endereco = RES
mockStore.setState(initialState)
expect(component.endereco).toEqual(RES)
});
});
我整天都在尝试在 jasmine 中做一个简单的测试,但我认为我做错了什么。我有一段代码想测试,但我不能进去。我试图遵循 nrgx 7 文档,但我失败了。
下面的单元测试应该测试我的 enderecoFeatureSubscription。 store.setState({ cep: null, endereco: RES }) 对商店没有做任何事情,所以我的订阅没有做任何事情
let component: FormComponent;
let fixture: ComponentFixture<FormComponent>;
let store: MockStore<ICepState>
const initialState = {
cep: null, endereco: null
};
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [FormComponent],
imports: [StoreModule.forRoot({}),],
providers: [
provideMockStore({ initialState: CEPSTATE })
]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(FormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
store = TestBed.get(Store);
});
it('should test enderecoFeatureSubscription ', () => {
store.setState({ cep: null, endereco: RES })
expect(component.endereco).toEqual(RES)
});
组件
private enderecoFeatureSubscription = this.store.pipe(select(enderecoFeatureSelector)).subscribe((endereco: IEndereco | any) => {
if (!endereco) {
return;
}
this.endereco = endereco
})
如果你能帮助我谢谢你,因为我已经浪费了很多时间。
在 ngrx 版本中。 > 8.0.0,如果您在各自覆盖的选择器上使用 store.setState
,则有一种方法 store.refreshState
可以刷新状态。不幸的是,refreshState
方法在 ngrx 7 中不存在。有一个替代方法 - 您应该使用 store.overrideSelector
覆盖所需的选择器,就像这样 -
it('should test enderecoFeatureSubscription ', () => {
store.overrideSelector(enderecoFeatureSelector, <put you mocked value>
fixture.detectChanges(); //MAKE sure to remove fixture.detectChanges() from beforeEach
expect(component.endereco).toEqual(RES)
});
我对我的测试工作做了一些修改。 1 - 删除 'const initialState' 并从我的应用程序状态文件中导入。 2 - MockStore 的类型,我更改为我的应用程序状态类型 3 - 在测试中,我将新值设置为 'cepState.endereco' 并使用 initialState 调用 setState 4 - 我将 'store' 更改为 'mockStore',但这并没有什么区别 5 - 最后,我带来了正确的进口
看下面的代码:
describe('FormComponent', () => {
let component: FormComponent;
let fixture: ComponentFixture<FormComponent>;
let mockStore: MockStore<AppState>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [FormComponent],
imports: [
StoreModule.forRoot({ 'cepState': CepReducer })
],
providers: [provideMockStore({ initialState })]
}).compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(FormComponent);
component = fixture.componentInstance;
fixture.detectChanges();
mockStore = TestBed.get(Store);
});
it('should test new endereco state', () => {
initialState.cepState.endereco = RES
mockStore.setState(initialState)
expect(component.endereco).toEqual(RES)
});
});