나는 지난 며칠 동안 rxjs와 redux-observable을 가지고 놀았으며 Observable.ajax에 대한 테스트 방법을 찾기 위해 애썼다. 나는 브라우저에서 실행되지만이 농담으로 테스트하지 않을 경우 때 코드가 잘 작동Observable.ajax (redux-observable)는 어떻게 테스트 할 수 있습니까?
export function testApiEpic (action$) {
return action$.ofType(REQUEST)
.switchMap(action =>
Observable.ajax({ url, method })
.map(data => successTestApi(data.response))
.catch(error => failureTestApi(error))
.takeUntil(action$.ofType(CLEAR))
)
}
,
export const REQUEST = 'my-app/testApi/REQUEST'
export const SUCCESS = 'my-app/testApi/SUCCESS'
export const FAILURE = 'my-app/testApi/FAILURE'
export const CLEAR = 'my-app/testApi/CLEAR'
export function requestTestApi() {
return { type: REQUEST }
}
export function successTestApi (response) {
return { type: SUCCESS, response }
}
export function failureTestApi (error) {
return { type: FAILURE, error }
}
export function clearTestApi() {
return { type: CLEAR }
}
https://jsonplaceholder.typicode.com/에 요청을 만들려면 다음 서사시를 가지고있다. 내가 시도가
,
1) https://redux-observable.js.org/docs/recipes/WritingTests.html에 따라 검사를 만듭니다. store.getActions()는 {type : REQUEST} 만 반환합니다.
const epicMiddleware = createEpicMiddleware(testApiEpic)
const mockStore = configureMockStore([epicMiddleware])
describe.only('fetchUserEpic',() => {
let store
beforeEach(() => {
store = mockStore()
})
afterEach(() => {
epicMiddleware.replaceEpic(testApiEpic)
})
it('returns a response,() => {
store.dispatch({ type: REQUEST })
expect(store.getActions()).toEqual([
{ type: REQUEST },
{ type: SUCCESS, response }
])
})
})
2) Redux-observable: failed jest test for epic을 기반으로 테스트를 만듭니다.
과 함께 반환됩니다. 시간 초과 - jasmine.DEFAULT_TIMEOUT_INTERVAL에 지정된 제한 시간 내에 비동기 콜백이 호출되지 않았습니다.
it('returns a response', (done) => {
const action$ = ActionsObservable.of({ type: REQUEST })
const store = { getState:() => {} }
testApiEpic(action$, store)
.toArray()
.subscribe(actions => {
expect(actions).to.deep.equal([
{ type: SUCCESS, response }
])
done()
})
})
사람은 Observable.ajax을 테스트하는 올바른 방법이 무엇인지 저를 지적 할 수 있습니까?