0
이유가 확실하지 않지만 테스트가 실패하면 Expected mock function to have been called.
이라고 표시됩니다. 전화를 걸려면 addFundingSource
모의를 어떻게 설정해야합니까? 나는 다른 모듈 (예 : jest.mock('../../../../_helpers/alerts');
)과 마찬가지로 전체 모듈을 jest.mock
수 없으며 나머지 모듈도 해당 모듈에서 사용되기 때문에 사용할 수 없습니다. 명명 된 수출 addFundingSource
실제로 조롱되는 경우 귀하의 예제에서fetch-mock으로 jest mock 설정
fetchFundingSource.js
export const addFundingSource =() => {
...
}
export const fetchFundingSource = (history, onNewPaymentClose) => {
let config = {
....
};
return async (dispatch) => {
dispatch(fetchFundingSourceRequest());
try {
const response = await fetch(fundingSourceEndpoint, config);
const jsonResponse = await response.json();
if (!response.ok) {
showErrorAlert(jsonResponse);
dispatch(fetchFundingSourceError(jsonResponse));
throw new Error(response.statusText);
}
dispatch(fetchFundingSourceSuccess(jsonResponse.count, jsonResponse.results));
addFoundingSource(jsonResponse.results, history, onNewPaymentClose);
} catch (error) {
if (process.env.NODE_ENV === 'development') {
console.log('Request failed', error);
}
}
};
테스트
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import * as fundingSource from '../../../../dashboard/global/ducks/fetchFundingSource';
import { fundingSourceEndpoint } from '../../../../_api/companyHelpers';
import { createMemoryHistory } from 'history';
jest.mock('../../../../_api/companyHelpers');
jest.mock('../../../../_helpers/alerts');
jest.mock('../../../../_api/authHelpers');
const middlewares = [thunk];
const createMockStore = configureMockStore(middlewares);
describe('fetchFundingSource',() => {
beforeEach(() => { fetch.resetMocks(); });
const history = createMemoryHistory('/dashboard');
const onNewPaymentClose = {};
fundingSource.addFundingSource = jest.fn(); //Mock setup.
it('dispatches the correct actions on successful fetch request',() => {
const store = createMockStore({});
fetch.mockResponse(JSON.stringify({ count, results }));
const expectedActions = [
fetchFundingSourceRequestObject,
fetchFundingSourceSuccessObject
];
return store.dispatch(fundingSource.fetchFundingSource(history, onNewPaymentClose))
.then(() => {
expect(fundingSource.addFundingSource).toBeCalled(); //THIS FAILS
expect(store.getActions()).toEqual(expectedActions);
});
});
});
이것은 모듈의 한 기능 만 모의하고 다른 기능은 변경하지 않기 때문에 좋은 팁입니다. 그러나 어떤 이유로 그것은 작동하지 않습니다. 그에 따라 내 질문이 업데이트되었습니다. 좀 봐 주시겠습니까? –
이전 댓글도 참조하십시오. 필자는 테스트 설정에서 콘솔을 로깅 할 때 함수가 조롱받는 것처럼 보이지만 여전히 원래 함수를 호출한다는 점을 잊어 버렸습니다. 비동기 호출 가져 오기 때문일 수 있습니까? –
올바른 타이밍으로 모의문을 설정하려면 beforeEach 또는 직접 호출 테스트에 선언해야합니다. –