测试ngxs状态类

时间:2018-11-07 15:32:31

标签: ngxs

您将如何用角度测试此状态类? 是否可以使用TestBed。您必须创建一个测试平台并提供默认状态吗? 任何帮助将不胜感激。谢谢

    @State<SomeModel>({
        name: 'providersubtypes',
        defaults: {
            prop: {} as Type,
        },
    })
    export class SomeState {

        constructor(private readonly alert: AlertService, private readonly shSrv: SharedService) { }

        @Selector()
        static getProviderSubTypes(state: SomeModel) {
            return state.subtypes;
        }

        @Action(FetchProviderSubTypes)
        fetchProviderSubTypes(context: StateContext<SomeModel>, action: FetchProviderSubTypes) {
            this.shSrv.getDataAction(action.url).subscribe(d => {
                context.patchState({
                    ...context.getState(),
                    subtypes: d,
                });
            });
        }
}

1 个答案:

答案 0 :(得分:2)

看看框架的文档: https://ngxs.gitbook.io/ngxs/recipes/unit-testing

您可以将selectOnce更改为selectSnapshot以避免使用可观察对象,例如:

it('it toggles feed', async(() => {
  store.dispatch(new FeedAnimals());
  
  const feed = store.selectSnapshot(state => state.zoo.feed);
  expect(feed).toBe(true);
}));