首页 > redux 异步action测试问题

redux 异步action测试问题

在redux英文文档中,如下测试异步的action

import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import * as actions from '../../actions/counter'
import * as types from '../../constants/ActionTypes'
import nock from 'nock'
import expect from 'expect' // You can use any testing library

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

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

  it('creates FETCH_TODOS_SUCCESS when fetching todos has been done', () => {
    nock('http://example.com/')
      .get('/todos')
      .reply(200, { body: { todos: ['do something'] }})

    const expectedActions = [
      { type: types.FETCH_TODOS_REQUEST },
      { type: types.FETCH_TODOS_SUCCESS, body: { todos: ['do something']  } }
    ]
    const store = mockStore({ todos: [] })

    return store.dispatch(actions.fetchTodos())
      .then(() => { // return of async actions
        expect(store.getActions()).toEqual(expectedActions)
      })
  })
})

其中,这段代码的作用是什么,加入与移除对测试结果并没有影响?,测试执行的都是原来action中的方法与自己设定的预期值相比,好像并没有用到nock中的数据,很困惑,望解答。

 nock('http://example.com/')
      .get('/todos')
      .reply(200, { body: { todos: ['do something'] }})

还有一点,在实际总做登陆的redux异步测试时,每次服务器返回的json数据中,token的信息值都是不一样的,这样该如何编写测试?

【热门文章】
【热门文章】