我正在尝试在 redux 应用程序中测试 api 调用。代码几乎遵循 redux 文档的Async Action Creators部分中概述的模式:
http://redux.js.org/docs/recipes/WritingTests.html
它的要点是您使用redux-mock-store来记录和断言任何触发的操作。
这是整个测试,使用 nock 模拟 api 调用:
import React from 'React'
import ReactDOM from 'react-dom'
import expect from 'expect';
import expectJSX from 'expect-jsx';
import TestUtils from 'react-addons-test-utils'
import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import nock from 'nock'
expect.extend(expectJSX);
import * as types from '../../constants/Actions'
describe('Async Search Actions', () => {
const thunkMiddleware = [ thunk ];
/* use redux-mock-store here */
const mockStore = configureMockStore(thunkMiddleware);
describe('The fetchArtistData action creator should', () => {
afterEach(() => {
nock.cleanAll()
})
it('Should fire off a ARTIST action when fetch is done', (done) => {
nock('http://ws.audioscrobbler.com')
.get('/2.0/')
.query({method: 'artist.search', artist: 'ho', api_key: 'abc123', format: 'json', limit: 5})
.reply(200,
{
fake: true
}
)
const expectedActions = [
{ type: types.ARTIST, artists: {
fake: true
}
}
];
let store = mockStore([], expectedActions, done);
store.dispatch(fetchArtist('ho'))
});
});
});
但似乎在运行测试时调用了真正的 lastFm api......从 lastFm 返回真实数据而不是假的 nock 响应。
这是动作创建者本身:
export function fetchArtist(search) {
return dispatch => {
return fetch(`http://ws.audioscrobbler.com/2.0/?method=artist.search&artist=${search}&api_key=abc123&format=json&limit=5`)
.then(handleErrors)
.then(response => response.json())
.then(json => { dispatch(ArtistData(searchTerm, json)) })
.catch(handleServerErrors)
}
}
断言失败,因为实时 lastFM 响应与我根据expectedActions
对象所期望的响应不同..
我尝试将 nock 分配给一个变量并将其注销。日志显示:
Nock 似乎将端口 80 添加到 url,不确定这是否会导致实际 API 未被模拟:
keyedInterceptors: Object{GET http://ws.audioscrobbler.com:80/2.0/?
method=artist.search&artist=john&api_key=abc123&format=json&limit=5
有什么想法吗?