在节点应用程序的异步 redux 操作中模拟传出请求不起作用
Mocking an outgoing request inside an async redux action in a node app doesn't work
我刚刚开始使用 node 和 redux 构建一个小型应用程序,方法是添加到 react-redux-starter-kit。
我现在正在尝试测试执行 API 调用的异步操作,同时严格遵守 redux example.
我正在使用包 isomorphic-fetch to perform the request and fetch-mock 来模拟它,但是当我 运行 我的测试时,它对我的 API.
执行了一个真实的请求
我已经注意到,当我在 it 块中执行 API 调用时,fetch-mock 按预期工作,但实际上我想测试一个 导入函数 来执行 API 呼叫.
我需要做什么才能使其也适用于导入的功能?
我的动作是这样的:
require('es6-promise').polyfill()
const fetch = require('isomorphic-fetch')
export const authenticateUserCredentials = ({email, password}) => {
return (dispatch) => {
return fetch('http://localhost:3005/v1/sign_in', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email,
password
})
}).then(response => response.json())
.then(json => {
console.log('json', json)
})
.catch((reason) => {
console.log('CATCHED ERROR:', reason.name, reason.message)
})
}
}
export const actions = {
authenticateUserCredentials
}
在我的规范文件中:
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import { actions } from 'redux/modules/session'
const fetchMock = require('fetch-mock')
const middlewares = [ thunk ]
const mockStore = configureMockStore(middlewares)
describe('(Async Action Creator) Authenticate user credentials', function () {
const EMAIL = 'a@b.de'
const TOKEN = '893irnjkklfnt'
const PASSWORD = 'foobar'
beforeEach(function () {
fetchMock.mock(
'http://localhost:3005/v1/sign_in',
'POST', {
status: 200,
body: '{"email":"' + EMAIL + '","token":"' + TOKEN + '"}'
}
)
})
afterEach(function () {
fetchMock.restore()
})
it('creates SIGN_IN when credentials are valid', (done) => {
const initialState = {}
const expectedActions = [
{ type: AUTHENTICATING, payload: undefined },
{ type: SIGN_IN, payload: {email: EMAIL, token: TOKEN} }
]
const store = mockStore(initialState, expectedActions, () => {
return done()
})
store.dispatch(
actions.authenticateUserCredentials({
email: EMAIL,
password: PASSWORD
})
)
})
})
使用这个包解决了问题:https://github.com/spaceviewinc/fetch-mock-forwarder.
更好的解决方案是不要将 isomorphic-fetch
分配给常量,因为它已经将自己分配为全局变量。 fetch-mock
被设计为与 fetch
一起工作作为一个全局的,因为这就是标准所说的 fetch
应该是的。可以让它与分配给其他变量的 fetch
一起工作,但这意味着跳过不必要的箍。
我刚刚开始使用 node 和 redux 构建一个小型应用程序,方法是添加到 react-redux-starter-kit。
我现在正在尝试测试执行 API 调用的异步操作,同时严格遵守 redux example.
我正在使用包 isomorphic-fetch to perform the request and fetch-mock 来模拟它,但是当我 运行 我的测试时,它对我的 API.
执行了一个真实的请求
我已经注意到,当我在 it 块中执行 API 调用时,fetch-mock 按预期工作,但实际上我想测试一个 导入函数 来执行 API 呼叫.
我需要做什么才能使其也适用于导入的功能?
我的动作是这样的:
require('es6-promise').polyfill()
const fetch = require('isomorphic-fetch')
export const authenticateUserCredentials = ({email, password}) => {
return (dispatch) => {
return fetch('http://localhost:3005/v1/sign_in', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email,
password
})
}).then(response => response.json())
.then(json => {
console.log('json', json)
})
.catch((reason) => {
console.log('CATCHED ERROR:', reason.name, reason.message)
})
}
}
export const actions = {
authenticateUserCredentials
}
在我的规范文件中:
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import { actions } from 'redux/modules/session'
const fetchMock = require('fetch-mock')
const middlewares = [ thunk ]
const mockStore = configureMockStore(middlewares)
describe('(Async Action Creator) Authenticate user credentials', function () {
const EMAIL = 'a@b.de'
const TOKEN = '893irnjkklfnt'
const PASSWORD = 'foobar'
beforeEach(function () {
fetchMock.mock(
'http://localhost:3005/v1/sign_in',
'POST', {
status: 200,
body: '{"email":"' + EMAIL + '","token":"' + TOKEN + '"}'
}
)
})
afterEach(function () {
fetchMock.restore()
})
it('creates SIGN_IN when credentials are valid', (done) => {
const initialState = {}
const expectedActions = [
{ type: AUTHENTICATING, payload: undefined },
{ type: SIGN_IN, payload: {email: EMAIL, token: TOKEN} }
]
const store = mockStore(initialState, expectedActions, () => {
return done()
})
store.dispatch(
actions.authenticateUserCredentials({
email: EMAIL,
password: PASSWORD
})
)
})
})
使用这个包解决了问题:https://github.com/spaceviewinc/fetch-mock-forwarder.
更好的解决方案是不要将 isomorphic-fetch
分配给常量,因为它已经将自己分配为全局变量。 fetch-mock
被设计为与 fetch
一起工作作为一个全局的,因为这就是标准所说的 fetch
应该是的。可以让它与分配给其他变量的 fetch
一起工作,但这意味着跳过不必要的箍。