测试爱可信与兴农要求,与终极版和噶

测试爱可信与兴农要求,与终极版和噶

问题描述:

你好了Redux文档中测试他们有这样的例子来测试API调用:测试爱可信与兴农要求,与终极版和噶

import configureMockStore from 'redux-mock-store' 
import thunk from 'redux-thunk' 
import * as actions from '../../actions/counter' 
import * as types from '../../constants/ActionTypes' 
import nock from 'nock' 

const middlewares = [ thunk ] 
const mockStore = configureMockStore(middlewares) 

describe('async actions',() => { 
    afterEach(() => { 
    nock.cleanAll() 
    }) 

    it('creates FETCH_TODOS_SUCCESS when fetching todos has been done', (done) => { 
    nock('http://example.com/') 
     .get('/todos') 
     .reply(200, { body: { todos: ['do something'] }}) 

    const expectedActions = [ 
     { type: types.FETCH_TODOS_REQUEST }, 
     { type: types.FETCH_TODOS_SUCCESS, body: { todos: ['do something'] } } 
    ] 
    const store = mockStore({ todos: [] }, expectedActions, done) 
    store.dispatch(actions.fetchTodos()) 
    }) 
}) 

我使用的是业力测试环境,我想我可以不要用诺克来测试这个。所以我正在考虑用Sinon来测试它。麻烦是我不明白我将如何测试使用这个,因为我没有传递回调到我的API函数调用。我使用axios来调用我的外部API。

+0

对此有什么更新?你设法解决它吗? – anoop

因为在我的应用程序中,我不是专家,因为在我的应用程序中我分别测试所有这些东西(动作创建者,API调用nock嘲笑服务,异步行为感谢saga,但是在redux文档中代码看起来像这样

const store = mockStore({ todos: [] }) 

    return store.dispatch(actions.fetchTodos()) 
     .then(() => { // return of async actions 
     expect(store.getActions()).toEqual(expectedActions) 
     }) 

于是派遣返回你的异步操作,你必须在将要执行的,当你异步操作会解决。Nock'ing端点应该只是罚款功能通过检测。

对于本你应该使用axios-mock-adapter

例子:

import MockAdapter from 'axios-mock-adapter'; 
import axios from 'axios'; 
import thunk from 'redux-thunk'; 
import configureMockStore from 'redux-mock-store'; 
import * as actionTypes from './userConstants'; 
import * as actions from './userActions'; 


const mockAxios = new MockAdapter(axios); 
const mockStore = configureMockStore(middlewares); 

describe('fetchCurrentUser',() => { 
    afterEach(() => { 
    mockAxios.reset(); 
    }); 

    context('when request succeeds',() => { 
    it('dispatches FETCH_CURRENT_USER_SUCCESS',() => { 
     mockAxios.onGet('/api/v1/user/current').reply(200, {}); 

     const expectedActions = [ 
     { type: actionTypes.SET_IS_FETCHING_CURRENT_USER }, 
     { type: actionTypes.FETCH_CURRENT_USER_SUCCESS, user: {} } 
     ]; 

     const store = mockStore({ users: Map() }); 

     return store.dispatch(actions.fetchCurrentUser()).then(() => 
     expect(store.getActions()).to.eql(expectedActions) 
    ); 
    }); 
    });