2017-03-08 52 views
0

我是新来Redux的测试,并已尝试备份填补测试的应用程序,我做出很抱歉,如果这是完全错误的方式去与箭扣和测试终极版 - 模拟商店。与箭扣和终极版,模拟店误差终极版动作测试

//Action in authAction.js 
export function fetchMessage() { 
    return function(dispatch) { 
    axios.get(ROOT_URL, { 
     headers: { authorization: localStorage.getItem('token') } 
    }) 
     .then(response => { 
     console.log("hi") 
     dispatch({ 
      type: FETCH_MESSAGE, 
      payload: response.data.message 
     }); 
     }) 
     .catch(response => { 
     console.log(response) 
     //callingRefresh(response,"/feature",dispatch); 
     }); 
    } 
} 

这是方法,它似乎越来越调用,但通常去的头不匹配诺克失败原因渔获原因。

//authActions_test.js 
import nock from 'nock' 
import React from 'react' 
import {expect} from 'chai' 
import configureMockStore from 'redux-mock-store' 
import thunk from 'redux-thunk' 

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

import * as actions from '../../src/actions/authActions'; 
const ROOT_URL = 'http://localhost:3090'; 

describe('actions',() => { 

    beforeEach(() => { 
     nock.disableNetConnect(); 
     localStorage.setItem("token", '12345'); 
    }); 

    afterEach(() => { 
     nock.cleanAll(); 
     nock.enableNetConnect(); 
    }); 


    describe('feature',() => { 

    it('has the correct type',() => { 
     var scope = nock(ROOT_URL).get('/',{reqheaders: {'authorization': '12345'}}).reply(200,{ message: 'Super secret code is ABC123' }); 
     const store = mockStore({ message: '' }); 

     store.dispatch(actions.fetchMessage()).then(() => { 
     const actions = store.getStore() 
     expect(actions.message).toEqual('Super secret code is ABC123'); 
    }) 


    }); 
    }); 
}); 

即使当标题被移除并且nock截取了该呼叫。我每次

TypeError: Cannot read property 'then' of undefined 
    at Context.<anonymous> (test/actions/authActions_test.js:43:24) 

回答

1

你不能从Axios公司以连锁的承诺返回then电话到收到此错误。

变化形实转换是这样的:

//Action in authAction.js 
export function fetchMessage() { 
    return function(dispatch) { 
    return axios.get(ROOT_URL, { 
     headers: { authorization: localStorage.getItem('token') } 
    }) 
     .then(response => { 
     console.log("hi") 
     dispatch({ 
      type: FETCH_MESSAGE, 
      payload: response.data.message 
     }); 
     }) 
     .catch(response => { 
     console.log(response) 
     //callingRefresh(response,"/feature",dispatch); 
     }); 
    } 
} 

您可能还需要改变测试,这样的承诺结算前,它不会通过。如何做到这一点取决于你使用的测试库。如果您使用摩卡,请看this answer

附注:我不知道,如果你有其他的单元测试,分别测试行动的创建者的减速,但这是测试这些非常综合的方式。一个终极版的一大优势是多么容易的机器的每个单独的齿轮可以单独进行测试,以对方。

+0

这个测试主要是为了正确地确保所有的行动过程中的反应,并与修正行为产生回应。我打算在另一个文件测试减速,以确保一旦他们接受正确的负载和输入状态看起来是正确的后 – Mike