Redux:如何对调度另一个异步操作的异步操作进行单元测试

时间:2016-04-15 22:03:59

标签: unit-testing asynchronous ecmascript-6 action redux

我很难尝试测试派遣另一个异步函数的异步函数:

export function validateVerifyCode(payload, pin) {
  return dispatch => {
    dispatch(requesting());
    return fetch(`${BASE_URL}/register`).then(resp => {
        return resp.json()
      })
      .then(json => {
        dispatch(hasSubscriptionCheck(json));
        }
      })
      .catch(error => dispatch(hasError(error)))
  }
}

function hasSubscriptionCheck(payload) {
  return dispatch => {
    dispatch(requesting());
    return fetch(`${BASE_URL}/subscriptions`)
      .then(resp => resp.json())
      .then(json => {
        if (json.length == 0) {
          dispatch(handleStripeSubmit(payload));
        }
      })
      .catch(error => dispatch(hasError(error)));
  }
}

问题出在我的单元测试中,它实际上并没有调用hasSubscriptionCheck:

  it('validates the verify code', (done) => {
    const stub = sinon.stub(Stripe.card, 'createToken');
    stub.onCall(0).returns(new Promise((resolve) => {
      resolve({});
    }));


    nock(BASE_URL)
      .defaultReplyHeaders({
        'Content-Type': 'application/json',
        'Authentication': token
      })
      .post('/register')
      .reply(200, {id: 'userid1234'})
      .get('/^subscriptions')
      .reply(200, []);


    const store = mockStore({});

    const payload = {
...
    };

    store.dispatch(actions.validateVerifyCode(payload, '123456'))
      .then(() => {
        console.log(store.getActions());
      })
      .then(done)
      .catch(done);
  })

奇怪的是,当我在console.log store.getActions()时,我看到了2个动作:

[ { type: 'REQUESTING' }, { type: 'REQUESTING' } ]

我如何告诉测试框架调用第二次获取?

0 个答案:

没有答案