0

我遵循了有关测试 ngxs 选择器的官方文档(https://ngxs.gitbook.io/ngxs/recipes/unit-testing#testing-selectors),但是它没有涵盖如何对使用createSelector.

我的普通选择器只是将状态作为参数获取,因此我可以通过传递准备好的状态并比较输出来轻松测试它。

@Selector()
static nachweise(state: NachweisStateModel) {
  return state.nachweise;
}



//Setup state   
const state = {...};

//Compare expectations
expect(NachweisState.nachweise(state)).toEqual(...);

我的动态选择器如下所示:

@Selector()
static nachweisById(id: string) {
  return createSelector([NachweisState], state => {
    return state.nachweise.find(nachweis => nachweis.id === id);
  });
}

它获得的唯一参数是它选择的 id,而不是状态。状态是通过将其指定为第一个参数自动传入的,createSelector我不知道应该如何测试这个选择器。

4

1 回答 1

2

似乎文档已更新

it('should select requested animal names from state', () => {
  const zooState = {
    animals: [
      { type: 'zebra', name: 'Andy'},
      { type: 'panda', name: 'Betty'},
      { type: 'zebra', name: 'Crystal'},
      { type: 'panda', name: 'Donny'},
    ]
  };
  const value = ZooSelectors.animalNames('zebra')(zooState);
  expect(value).toEqual(['Andy', 'Crystal']);
});
于 2019-01-11T09:51:18.770 回答