2017-04-30 9 views
1

사가에서 catch 문을 테스트 할 때 문제가 있습니다. 나는 콘솔에 출력 된 Error: error으로 아래 테스트에서 실패하는 Mocha와 chai를 사용하고 있습니다.redux-saga에서 try-catch 테스트

saga.js

function* submitCredentials(action) { 
    try { 

    } catch(e) { 
    yield put({type: LOGIN_FAIL, message: e.message}) 
    } 
} 

test.js

it('should catch errors',() => { 
    const generator = submitCredentials(); 
    const error = new Error('error'); 

    expect(generator.throw(error).value, put({ 
    type: actions.LOGIN_FAIL, error 
    })); 
}); 
+0

이 기대 라인이 기대'할 필요가 없습니다 (generator.throw (오류) .value) .to.equal ((넣어 { 유형 : actions.LOGIN_FAIL, '오류' 을}));' – BarthesSimpson

+0

또는 (유형 : actions.LOGIN_FAIL, {message : 'error'} )));) – BarthesSimpson

답변

1

이처럼보십시오 :

const generator = submitCredentials(); 
const error = new Error('error'); 
generator.next().value 
output = generator.throw(error).value 
let expected = put({ 
    type: actions.LOGIN_FAIL, error 
}) 
expect(output).toEqual(expected) 
+0

이렇게하면 동일한 결과를 얻게됩니다 (generator.throw (error) .value). 내 예제로 – Mantis

+0

오류 메시지가 무엇입니까? – andresmijares25

0

발전기는 오류 메시지와 함께 작업을 전달하지만, 테스트에 액션 페이로드가 에러 객체라고 기대하고 있습니다. 따라서 예상대로 변경하면

put({ 
    type: actions.LOGIN_FAIL, {message: 'error'} 
}) 

으로 변경해야합니다.