2017-07-10 17 views
5

में Axios मैं में इस कार्रवाई की है कि कैसे परीक्षण करते हैं प्रतिक्रियामैं हंसी

export function fetchPosts() { 
    const request = axios.get(`${WORDPRESS_URL}`); 
    return { 
     type: FETCH_POSTS, 
     payload: request 
    } 
} 

कैसे मैं इस मामले में Axios परीक्षण करते हैं? जेस्ट के पास एसिंक कोड के लिए साइट पर इस उपयोग का मामला है जहां वे एक नकली फ़ंक्शन का उपयोग करते हैं लेकिन मुझे नहीं पता कि मैं अक्षरों के साथ ऐसा कर सकता हूं? रेफरी: https://facebook.github.io/jest/docs/tutorial-async.html

मैं यह किया है अब तक परीक्षण करने के लिए है कि यह सही प्रकार

it('should dispatch actions with the correct type',() => { 
    store.dispatch(fetchPosts()); 
    let action = store.getActions(); 
    expect(action[0].type).toBe(FETCH_POSTS); 
}); 

मैं पता नहीं कैसे नकली डेटा और परीक्षण है कि यह फिर भी रिटर्न में पारित करने के लिए है लौटा रहा है, किसी को भी किसी भी मिल गया है विचार?

अग्रिम में धन्यवाद

+1

https: // github।com/mzabriskie/moxios –

+0

मेरे परीक्षण के लिए मैंने निम्न का उपयोग किया है: https://gist.github.com/knee-cola/00641c39a268f0b96f1842640b87f3a2। यह टाइपस्क्रिप्ट में लिखा गया है, लेकिन आसानी से वेनिला जेएस –

उत्तर

12

मैंने अक्ष-मैक-एडाप्टर का उपयोग किया। इस मामले में सेवा ./chatbot में वर्णित है। नकली एडाप्टर में आप निर्दिष्ट करते हैं कि यूआरएल कब मारा जाता है।

import axios from 'axios'; 
import MockAdapter from 'axios-mock-adapter'; 
import chatbot from './chatbot'; 

describe('Chatbot',() => { 
    it('returns data when sendMessage is called', done => { 
     var mock = new MockAdapter(axios); 
     const data = { response: true }; 
     mock.onGet('https://us-central1-hutoma-backend.cloudfunctions.net/chat').reply(200, data); 

     chatbot.sendMessage(0, 'any').then(response => { 
      expect(response).toEqual(data); 
      done(); 
     }); 
    }); 
}); 

आप इसे यहाँ पूरे उदाहरण देख सकते हैं:

सेवा: https://github.com/lnolazco/hutoma-test/blob/master/src/services/chatbot.js

टेस्ट: https://github.com/lnolazco/hutoma-test/blob/master/src/services/chatbot.test.js

+0

में परिवर्तित किया जा सकता है धन्यवाद लुइस ने मुझे – Adear

1

मैं nock के साथ इस किया है, इसलिए जैसे:

import nock from 'nock' 
import axios from 'axios' 
import httpAdapter from 'axios/lib/adapters/http' 

axios.defaults.adapter = httpAdapter 

describe('foo',() => { 
    it('bar',() => { 
     nock('https://example.com:443') 
      .get('/example') 
      .reply(200, 'some payload') 

     // test... 
    }) 
}) 
+0

को बहुत मदद की है, मैंने कोशिश की लेकिन मुझे लगता है कि अक्षरों को nock.https के साथ अच्छी तरह से नहीं खेलता है: //github.com/node- नॉक/नॉक/अंक/69 9 लेकिन आपकी मदद के लिए धन्यवाद कोई भी कम – Adear

1

मैं कर सकता है कि चरणों का पालन:

  1. परीक्षण

नकली इच्छा पर मेरी कार्यान्वित मॉड्यूल का उपयोग कर एक फ़ोल्डर नकली

  • एक axios.js नकली फ़ाइल
  • लागू बनाने स्वचालित रूप से

    नकली मोड का उदाहरण ule:

    describe('SignInUser',() => { 
        var history = { 
        push: function(str) { 
         expect(str).toEqual('/feed'); 
        } 
        } 
    
        it('Dispatches authorization',() => { 
        let mock = new MockAdapter(axios); 
        mock.onPost(`${ROOT_URL}/auth/signin`, { 
         email: '[email protected]', 
         password: 'test' 
        }).reply(200, {token: 'testToken' }); 
    
        const expectedActions = [ { type: types.AUTH_USER } ]; 
        const store = mockStore({ auth: [] }); 
    
        return store.dispatch(actions.signInUser({ 
         email: '[email protected]', 
         password: 'test', 
        }, history)).then(() => { 
         expect(store.getActions()).toEqual(expectedActions); 
        }); 
    
    }); 
    

    में:

    module.exports = { 
        get: jest.fn((url) => { 
         if (url === '/something') { 
          return Promise.resolve({ 
           data: 'data' 
          }); 
         } 
        }), 
        post: jest.fn((url) => { 
         if (url === '/something') { 
          return Promise.resolve({ 
           data: 'data' 
          }); 
         } 
         if (url === '/something2') { 
          return Promise.resolve({ 
           data: 'data2' 
          }); 
         } 
        }), 
        create: jest.fn(function() { 
         return this; 
        }) 
    }; 
    
  • 0

    redux प्रलेखन for async testing में mockfetch उदाहरण के स्थान पर Axios-नकली एडाप्टर का उपयोग करने के लिए देख रहे लोगों के लिए, मैं सफलतापूर्वक निम्नलिखित

    actions.test.js उपयोग किया signInUseractions/index.js:

    export const signInUser = ({ email, password }, history) => async dispatch => { 
        const res = await axios.post(`${ROOT_URL}/auth/signin`, { email, password }) 
        .catch(({ response: { data } }) => { 
         ... 
        }); 
    
        if (res) { 
        dispatch({ type: AUTH_USER });     // test verified this 
        localStorage.setItem('token', res.data.token); // test mocked this 
        history.push('/feed');       // test mocked this 
        } 
    } 
    
    में एक सफल केस का परीक्षण करने के लिए

    यह देखते हुए कि यह जस्ट के साथ किया जा रहा है, स्थानीय स्टोरेज कॉल को मजाक करना था। यह src/setupTests.js में था:

    const localStorageMock = { 
        removeItem: jest.fn(), 
        getItem: jest.fn(), 
        setItem: jest.fn(), 
        clear: jest.fn() 
    }; 
    global.localStorage = localStorageMock; 
    
    +0

    आपने स्थानीय स्टोरेजमोक –

    +0

    @Kermit_ice_tea 'src/setupTests.js' का उपयोग कैसे किया है, जेस्ट/एंजाइम परीक्षण के लिए वैश्विक सेटअप के रूप में create-react-app में प्रलेखित है । उस फ़ाइल में मैंने एक ऑब्जेक्ट बनाया है जिसे मैंने मनमाने ढंग से 'लोकस्टोरेजमोक' कहा है जिसमें डमी फ़ंक्शन (getItem, setItem) हैं। जादू नीचे है जहां मैंने इस ऑब्जेक्ट के बराबर 'global.localStorage' सेट किया है। मैं ऑब्जेक्ट स्थानीयस्टोरमॉक नामकरण के बजाय इसे एक पंक्ति में कर सकता था। इस पूरे सेटअप का उद्देश्य स्थानीय स्टोरेज से परीक्षण के दौरान तोड़ने से निपटने वाले किसी भी कोड से बचना था। – vapurrmaid

    संबंधित मुद्दे