我正在學習如何測試和使用一些示例作爲指南我試圖模擬登錄帖子。這個例子使用了對http調用的獲取,但是我使用了axios。這是我得到如何測試react-saga axios post
超時錯誤 - 異步回調不被jasmine.DEFAULT_TIMEOUT_INTERVAL
所有的答案,這個錯誤的指定的超時時間內調用了同取,我該怎麼辦辦這與愛可信
./saga
const encoder = credentials => Object.keys(credentials).map(key => `${encodeURIComponent(key)}=${encodeURIComponent(credentials[key])}`).join('&')
const postLogin = credentials => {
credentials.grant_type = 'password'
const payload = {
method: 'post',
headers: config.LOGIN_HEADERS,
data: encoder(credentials),
url: `${config.IDENTITY_URL}/Token`
}
return axios(payload)
}
function * loginRequest (action) {
try {
const res = yield call(postLogin, action.credentials)
utils.storeSessionData(res.data)
yield put({ type: types.LOGIN_SUCCESS, data: res.data })
} catch (err) {
yield put({ type: types.LOGIN_FAILURE, err })
}
}
function * loginSaga() {
yield takeLatest(types.LOGIN_REQUEST, loginRequest)
}
export default loginSaga
./login-test
const loginReply = {
isAuthenticating: false,
isAuthenticated: true,
email: '[email protected]',
token: 'access-token',
userId: '1234F56',
name: 'Jane Doe',
title: 'Tester',
phoneNumber: '123-456-7890',
picture: 'pic-url',
marketIds: [1, 2, 3]
}
describe('login-saga',() => {
it('login identity user', async (done) => {
// Setup Nock
nock(config.IDENTITY_URL)
.post('/Token', { userName: '[email protected]', password: 'xxxxx' })
.reply(200, loginReply)
// Start up the saga tester
const sagaTester = new SagaTester({})
sagaTester.start(loginSaga)
// Dispatch the event to start the saga
sagaTester.dispatch({type: types.LOGIN_REQUEST})
// Hook into the success action
await sagaTester.waitFor(types.LOGIN_SUCCESS)
// Check the resulting action
expect(sagaTester.getLatestCalledAction()).to.deep.equal({
type: types.LOGIN_SUCCESS,
payload: loginReply
})
})
})
你是怎麼做發電機的工作? – JoseAPL
第一次使用它們,所以仍然在學習 – texas697