如何在提供MockStore 的MockBuilder中重写选择器值
通常使用试验台
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [
ABCComponent,
MockComponent(YYYComponent)
],
providers: [
provideMockStore({
initialState: getMockAppState(),
selectors: [
{
selector: getValue,
value: true
}
]
})
]
});
storeStub = TestBed.inject(MockStore);
dispatchSpy = jest.spyOn(storeStub, 'dispatch');
});
it('.....', () => {
storeStub.overrideSelector(getValue, false)
})
您需要使用MockBuilder.provide
。
此外,因为您想要MockStore,所以需要避免对ngrx
模块进行嘲讽:https://ng-mocks.sudo.eu/guides/libraries/ngrx.
并且,您需要拆分初始化和配置:
beforeEach(() => {
return MockBuilder(ABCComponent)
.mock(YYYComponent)
.provide(
provideMockStore({
initialState: getMockAppState(),
selectors: [
{
selector: getValue,
value: true,
},
],
}),
);
});
// creating a factory
const factory = MockRenderFactory(ABCComponent);
beforeEach(() => factory.configureTestBed());
it('.....', () => {
const storeStub = TestBed.inject(MockStore);
const dispatchSpy = jest.spyOn(storeStub, 'dispatch');
// rendering ABCComponent
const fixture = factory();
storeStub.overrideSelector(getValue, false)
});