0
Jest를 사용하여 유닛 테스트를 시도하고 있는데 jest mock 모듈 (nodejs 세계의 rewire 또는 proxyquire에 해당)을 다루는 데 문제가 있습니다.Jest와 모킹 약속 기반 요청
실제로 일부 매개 변수로 조롱 된 모듈에서 스파이가 호출되었음을 테스트하려고합니다. 다음은 테스트하고 싶은 기능입니다.
NB : 현재 테스트는 "fetch (...)"부분에만 관련이 있습니다. 테스트 할 때 좋은 매개 변수를 사용하여 테스트했습니다.
export const fetchRemote = slug => {
return dispatch => {
dispatch(loading());
return fetch(Constants.URL + slug)
.then(res => res.json())
.then(cmp => {
if (cmp.length === 1) {
return dispatch(setCurrent(cmp[0]));
}
return dispatch(computeRemote(cmp));
});
};
};
반환 된 함수는 클로저 역할을하므로 모의하려는 노드 가져 오기 외부 모듈을 "캡처합니다".
it('should have called the fetch function wih the good const parameter and slug', done => {
const slug = 'slug';
const spy = jasmine.createSpy();
const stubDispatch =() => Promise.resolve({json:() => []});
jest.mock('node-fetch',() => spy);
const dispatcher = fetchRemote(slug);
dispatcher(stubDispatch).then(() => {
expect(spy).toHaveBeenCalledWith(Constants.URL + slug);
done();
});
});
편집 : 여기
내가 패스 녹색 만들기 위해 노력하고있어 시험의 첫 번째 대답은 테스트를 작성에 관한 많은 도움이, 내가 지금 다음과 같은 일이 :it('should have called the fetch function wih the good const parameter and slug', done => {
const slug = 'slug';
const stubDispatch =() => null;
const spy = jest.mock('node-fetch',() => Promise.resolve({json:() => []}));
const dispatcher = fetchRemote(slug);
dispatcher(stubDispatch).then(() => {
expect(spy).toHaveBeenCalledWith(Constants.URL + slug);
done();
});
});
그러나 자, 내가 가진 오류는 다음과 같습니다.
console.error node_modules/core-js/modules/es6.promise.js:117
Unhandled promise rejection [Error: expect(jest.fn())[.not].toHaveBeenCalledWith()
jest.fn() value must be a mock function or spy.
Received:
object: {"addMatchers": [Function anonymous], "autoMockOff": [Function anonymous], "autoMockOn": [Function anonymous], "clearAllMocks": [Function anonymous], "clearAllTimers": [Function anonymous], "deepUnmock": [Function anonymous], "disableAutomock": [Function anonymous], "doMock": [Function anonymous], "dontMock": [Function anonymous], "enableAutomock": [Function anonymous], "fn": [Function anonymous], "genMockFn": [Function bound getMockFunction], "genMockFromModule": [Function anonymous], "genMockFunction": [Function bound getMockFunction], "isMockFunction": [Function isMockFunction], "mock": [Function anonymous], "resetModuleRegistry": [Function anonymous], "resetModules": [Function anonymous], "runAllImmediates": [Function anonymous], "runAllTicks": [Function anonymous], "runAllTimers": [Function anonymous], "runOnlyPendingTimers": [Function anonymous], "runTimersToTime": [Function anonymous], "setMock": [Function anonymous], "unmock": [Function anonymous], "useFakeTimers": [Function anonymous], "useRealTimers": [Function anonymous]}]
이 구현에서 스파이를 볼 수 없습니다. 하지만 고맙습니다. 대답 : – mfrachet
'expect (spy)'대신'expect (spy.mock)'를 사용해 보셨습니까? –
그리고 당신은 당신의 시험에서 배정사를 돌려 보내야합니다. –