Nock不拦截redux动作创建者请求

时间:2016-10-17 15:19:46

标签: reactjs redux nock

我试图使用nock测试我的第一个动作创建者。我跟随redux documentation。这是我的代码

import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import nock from 'nock'

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

describe('(Action creator)', () => {
  beforeEach((done) => {
    nock.disableNetConnect();
    done()
  })

  afterEach((done) => {
    nock.cleanAll()
    nock.enableNetConnect();
    done()
  })

  describe('#loginUser', () => {
    it('should login user', () => {
      const initialState = {
        isFetching: false,
        isAuthenticated: false,
      };

      const expectedActions = [
        { type: LOGIN_REQUEST,
          isFetching: true,
          isAuthenticated: false,
          credentials: {
            email: 'test@test.com',
            password: 'test',
          }},
        { type: LOGIN_SUCCESS,
          isFetching: false,
          isAuthenticated: true,
          id_token: 'idtoken',
        }
      ]
      const store = mockStore(initialState)

      nock('http://localhost:4000')
        .post('/auth/login', '*')
        .reply(200, { body: { token: 'idtoken' } })

      return store
        .dispatch(loginUser({ email: 'test@test.com', password: 'test' }))
        .then(() => {
          expect(store.getActions()).to.deep.equal(expectedActions)
        })
    })
  })

测试在断言时失败,因为nock向服务器发出实际请求并接收真实令牌,而不是假令牌。以下是我在服务器端可以看到的日志

::ffff:127.0.0.1 - - [17/Oct/2016:15:05:28 +0000] "OPTIONS /auth/login HTTP/1.1" 204 -
::ffff:127.0.0.1 - - [17/Oct/2016:15:05:28 +0000] "POST /auth/login HTTP/1.1" 200 427

我错过了什么吗?为什么nock即使使用nock.disableNetConnect();

也会发出真实请求

0 个答案:

没有答案