我正在使用 Angular、NgRx 和 Jest。
我正在使用从商店读取的服务
export class MyFacade {
configuration$ = this.store.select(selectors.selectConfiguration);
}
我在我的规范文件中使用它来测试效果。我已经创建了像这样实现它的模拟类
export class MyMockFacade {
configuration$ = of(configObject);
}
我的规格文件看起来像这样:
describe('Config Effects', () => {
const mockStore = new MockStore();
....
const mockFacade = new MyMockFacade();
let actions;
let effects: ConfigEffects;
beforeAll(() => {
Object.defineProperty(window, 'location', {
configurable: true,
value: { reload: jest.fn() }
});
});
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
ConfigEffects,
HttpClientModule,
provideMockActions(() => actions),
{ provide: Store, useValue: mockStore },
...
{ provide: MyFacade , useValue: mockFacade },
]
});
effects = TestBed.inject(ConfigEffects);
});
});
我希望能够更改测试中配置$返回的值。我怎样才能实现它
it('should call ....', () => {
//I would like to be able to assigned new value here!
expect(effects.loadXXX$).toSatisfyOnFlush(() => {
...
});
});
将
configuration$
设为 BehaviorSubject
,以便您可以指定它应发出的值:
export class MyMockFacade {
configuration$ = new BehaviorSubject<ConfigObject>(null);
}
it('should call ....', () => {
mockFacade.configuration$.next(someConfigObject);
expect(effects.loadXXX$).toSatisfyOnFlush(() => {
...
});
});