我有一个带有 node/express 的模拟 api 设置
http://localhost:8080/api/lyric/
我正在使用 redux-observable 来获取 json:
const fetchLyricEpic = action$ =>
action$.ofType(FETCH_LYRIC)
.mergeMap(action =>
ajax.getJSON('/api/lyric')
.map(response => fetchLyricSuccess(response))
.catch(error => Observable.of(fetchLyricFail(error)))
)
问题是,当我测试时,我得到一个 api 错误而不是 200 使用nock
:
beforeEach(() => {
nock.disableNetConnect()
nock.enableNetConnect('127.0.0.1:3000/')
})
afterEach(() => {
nock.cleanAll()
nock.enableNetConnect()
epicMiddleware.replaceEpic(rootEpic)
})
test('it creates FETCH_SUCCESS when fetching the lyric has been done', () => {
const payload = {
status: 200,
response: {
'lyric': "Lyric"
}
}
nock('127.0.0.1:3000/')
.get('/api/lyric')
.reply(200, payload, {'Content-Type': 'application/json'})
我正在尝试遵循我在 nock 文档和问题列表中阅读的内容,但是,我的 nock api 调用一直失败。
Expected value to equal:
[{"type": "FETCH_LYRIC"}, {"payload": {"lyric": "Lyric"}, "type": "FETCH_LYRIC_SUCCESS"}]
Received:
[{"type": "FETCH_LYRIC"}, {"payload": {}, "type": "FETCH_LYRIC_ERROR"}]
我应该收到,FETCH_LYRIC_SUCCESS
而不是我看到:
[ { type: 'FETCH_LYRIC' },
{ type: 'FETCH_LYRIC_ERROR',
payload:
SyntaxError
at XMLHttpRequest.open (<user>/node_modules/jsdom/lib/jsdom/living/xmlhttprequest.js:486:15)
at XMLHttpRequest.tryCatcher (<user>/node_modules/rxjs/util/tryCatch.js:6:31)
at AjaxSubscriber.Object.<anonymous>.AjaxSubscriber.send (<user>/node_modules/rxjs/observable/dom/AjaxObservable.js:208:56)
at new AjaxSubscriber (<user>/node_modules/rxjs/observable/dom/AjaxObservable.js:180:14)
at AjaxObservable.Object.<anonymous>.AjaxObservable._subscribe (<user>/node_modules/rxjs/observable/dom/AjaxObservable.js:115:16)
at AjaxObservable.Object.<anonymous>.Observable._trySubscribe (<user>/node_modules/rxjs/Observable.js:57:25)
at AjaxObservable.Object.<anonymous>.Observable.subscribe (<user>/node_modules/rxjs/Observable.js:45:27)
at MapOperator.Object.<anonymous>.MapOperator.call (<user>/node_modules/rxjs/operator/map.js:54:23)
at Observable.Object.<anonymous>.Observable.subscribe (<user>/node_modules/rxjs/Observable.js:42:22)
at MapOperator.Object.<anonymous>.MapOperator.call (<user>/node_modules/rxjs/operator/map.js:54:23) } ]