2017-11-21 1 views
0

나는 아파치와 내 API 호출을 모의하려고하지만 어떤 이유로 그것은 작동하지 않습니다. 나는 이유를 정말로 이해하지 못한다. 누구나 아이디어가 있습니까?Jest API를 모의 호출하는 방법

불변

import { getStuff } from '../stuff'; 
import * as api from '../../util/api'; 

describe('Action getStuff',() => { 
     it('Should call the API to get stuff.',() => { 
      api.call = jest.fn(); 
      getStuff('slug')(() => {},() => {}); 
      expect(api.call).toBeCalled(); 
      jest.unmock('../../util/api.js'); 
     }); 
}); 

stuff.js REDUX 행동

내 test.js

import api from '@util/api'; 
import { STUFF, API } from '../constant'; 


export const getStuff = slug => (dispatch, getState) => { 
    const state = getState(); 
    api.call(API.STUFF.GET, (err, body) => { 
     if (err) { 
      console.error(err.message); 
     } else { 
      dispatch({ 
       type: STUFF.GET, 
       results: body, 
      }); 
     } 
    }, { 
     params: { slug }, 
     state 
    }); 
}; 

답변

1

가져 오기를 (시험은 원래 API 호출 기능이 아닌 모의를 호출 계속) 그래서 작동하지 않을 것입니다, 당신이해야 할 것은 전체 모듈을 조롱하는 것입니다. __mock__ 디렉토리 또는 간단히 :

jest.mock('../../util/api'); 
const { call } = require('../../util/api'); 
call.mockImplementation(() => console.log("some api call")); 
+0

어떻게 테스트 할 수 있습니까? (실수, 신체) => { if (err) { console.error (err.message); } else { 발송 ({ 유형 : STUFF.GET, 결과 : 본문, }); } } – Alexandre

+0

그 방법의 문제점은 무엇입니까? 파견의 경우에는 – Axnyff

+0

Thx에 메서드를 제공하는 것을 모의해야합니다. api.call = jest.fn (경로 = '', 콜백) => { 콜백 ({메시지 : 'some error'}, null); }); – Alexandre

관련 문제