我正在尝试使用nock
. 我正在关注 redux文档。这是我的代码
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();