在Redux/Mocha/Nock中测试服务器调用

在Redux/Mocha/Nock中测试服务器调用,redux,nock,Redux,Nock,我是测试新手,所以我甚至不确定这是否是我“应该”测试的东西,但下面是: 我将按照中的示例介绍如何为异步操作创建者编写测试。以下是我正在测试的代码: export function receiveRepresentatives(json) { return { type: RECEIVE_REPRESENTATIVES, representatives: json.objects } } export function getRepresentatives (zipcod

我是测试新手,所以我甚至不确定这是否是我“应该”测试的东西,但下面是:

我将按照中的示例介绍如何为异步操作创建者编写测试。以下是我正在测试的代码:

export function receiveRepresentatives(json) {
  return {
    type: RECEIVE_REPRESENTATIVES,
    representatives: json.objects
  }
}

export function getRepresentatives (zipcode) {
  return dispatch => {
    dispatch(changeFetching())
    return fetch('/api/representatives' + zipcode)
        .then(response => response.json())
        .then(json => dispatch(receiveRepresentatives(json))) 
  }
}
我的测试框架是带有nock和configureMockStore的mocha/chai。我想用nock来模拟我对/api/representation的调用,但我不知道怎么做

const middlewares = [ thunk ]
const mockStore = configureMockStore(middlewares)

describe('async actions', () => {
  afterEach(() => {
    nock.cleanAll()
  })

  it('creates RECEIVE_REPRESENTATIVES when fetching representatives has been done', (done) => {
    nock('http://localhost')
      .get('/api/representatives')
      .reply('200', { objects: { name: 'Barbara Lee'} } )

    const expectedActions = [
      { type: RECEIVE_REPRESENTATIVES, representatives: { objects: { name: 'Barbara Lee'} } }
    ]

    const store = mockStore({}, expectedActions, done)
    store.dispatch(getRepresentatives(94611))
      .then(() => {
        const actions = store.getActions()
        expect(actions[0].type).toEqual(receiveRepresentatives())

        done()
      })
  })
})

你们的考试怎么了?我看不到任何问题。问题在于您的expect,应该是expect(操作[0]);