Angular 2,ngrx,测试我的Reducer

时间:2018-04-03 14:07:40

标签: angular typescript jasmine rxjs ngrx

在我的Reducer文件中:

 case PumpActionTypes.EnterLocalMode:
  return commandOne.upsertOne(
    {id: action.payload.id, changes: { local: false }},
    state
  );

在我的测试中,我想测试以查看local已更改为false。 我写的:

  describe('EnterLocalMode action', () => {
    it('should handle Enter Local Mode from Remote Mode', () => {


    const {initialState} = MyReducer;

    const action = new MyActions.EnterLocalMode();

    const state = MyReducer.reducer(initialState, action);

    //Test that the changes in pump adaptor change to "local" = true
    expect(MyReducer.commandOne.upsertOne(changes:{local})).toEqual(true);

      });



});

但是,我不确定如何写我的期望陈述,现在是错误的:

expect(MyReducer.commandOne.upsertOne(changes:{local})).toEqual(true);

1 个答案:

答案 0 :(得分:1)

Reducer是一个纯函数,它根据statepreviousState返回一个新的action。所以你应该只测试它。

expect(PumpReducer.reducer({counter:1}, Increment).counter).toBe(2)

假设state = {counter:0},行动Incrementstate更改为{counter:1}

您应该测试所有类似的操作以及每个操作中的任何条件分支。

修改

case PumpActionTypes.EnterLocalMode:
  return pumpAdapter.upsertOne(
    {id: action.payload.id, changes: { local: false }},
    state
  ); 

所以你基于函数调用返回状态。然后

expect(PumpReducer.pumpAdapter.upsertOne(changes:{local})).toEqual(true);

看起来您正在测试使用upsertOne()调用changes:{local}或类似的情况。您可以使用jasmine spyon方法检查和测试函数调用的arguments