我正在使用 node nock 来测试我的 redux 异步操作。测试当前失败,输出如下:
AssertionError: expected [ Array(2) ] to deeply equal [ Array(2) ]
+ expected - actual
{
"type": "FETCH_MOVIES"
}
{
- "data": [undefined]
+ "data": [
+ {
+ "title": "Batman vs Superman"
+ }
+ ]
"type": "FETCH_MOVIES_SUCCESS"
}
]
动作.js:
import axios from 'axios';
import * as constants from '../constants/constants';
const MOVIES_API = 'https://api.themoviedb.org/3/discover/movie?api_key=xxx';
function fetchMoviesRequest() {
return {
type: constants.FETCH_MOVIES
};
}
function fetchMoviesSuccess(data) {
return {
type: constants.FETCH_MOVIES_SUCCESS,
data
};
}
function fetchMoviesError(ex) {
return {
type: constants.FETCH_MOVIES_ERROR,
ex
};
}
export function fetchMovies() {
return dispatch => {
dispatch(fetchMoviesRequest());
return axios.get(MOVIES_API)
.then(res => dispatch(fetchMoviesSuccess(res.data.results)))
.catch(ex => dispatch(fetchMoviesError(ex)));
};
}
action.spec.js :
import configureStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import * as actions from './actions';
import * as ActionTypes from '../constants/constants';
import nock from 'nock';
import { expect } from 'chai';
const MOVIES_API = 'https://api.themoviedb.org/3/discover/movie?api_key=xxx';
const middlewares = [ thunk ];
const mockStore = configureStore(middlewares);
describe('async actions', () => {
afterEach(() => {
nock.cleanAll();
});
it('creates FETCH_MOVIES_SUCCESS when fetching movies is complete', () => {
nock(MOVIES_API)
.log(console.log)
.get('/')
.reply(200, {data: {results: [{title: 'Batman vs Superman'}]}});
const expectedActions = [
{ type: ActionTypes.FETCH_MOVIES },
{ type: ActionTypes.FETCH_MOVIES_SUCCESS, data: {results: [{title: 'Batman vs Superman'}]}}
];
// const store = createStore(rootReducer, initialState, applyMiddleware(thunk));
const store = mockStore( { movies: {} });
return store.dispatch(actions.fetchMovies()).then(() => {
expect(store.getActions()).to.deep.equal(expectedActions);
});
});
});
这是我第一次使用 nock,所以我不确定我需要做什么来获得要定义的数据。