2017-03-10 2 views
1

을 던지는하지오이 JS는 내가 cucumber.js의 작은 테스트를 쓰고 있어요 실패

defineSupportCode(function({Given, When, Then}){ 
Given(/^I have a valid github username $/, function(callback){ 
    githubUserName = 'test'; 
    callback(); 
}); 

When('I call username api', function(callback){ 
    let url = githubUrl+githubUserName; 
    let response; 
    var data; 
    githubResponse = fetch(url) 
     .then(res => res.json()) 
     .then(json => {return json}); 
    callback(); 
}); 

Then('I get userdetails', function(){ 
    githubResponse.then(function(data){ 
     console.log(data.name); 
     expect(data.name).to.equal("abc"); 
    }); 
}); 

});

이제 테스트를 실행하면 통과합니다. 나는 심지어 콜백을 시도했다. 그러나 예외는 예외를 던지고 있지만 여전히 전달을 유지한다.

어디에서나 처리기가 누락 되었습니까? 저는이 프로그래밍 스타일에 대한 총체적인 초심자입니다.

+0

"가져 오기"기능이 무엇인지 모르겠지만 코드가 생각하는대로 수행한다고 가정하면 "다음 단계"에서 return githubResponse.then ...가 누락됩니다. 리턴 오이가 없으면 호출이 실행되고 완료 될 때까지 기다리지 않고 함수를 종료하므로 항상 성공합니다. – joniba

답변

1

아마도 문제는 fetch에 대한 spec이 404. So perhaps you need to write a wrapper around it to deal with this 일 때 실패하지 않는다고 말합니다. 여기 내 버전이 있습니다 :

function myFetch(url, options) { 
    if (options == null) options = {}; 
    return fetch(url, options).then(function(response) { 
    if (response.status >= 200 && response.status < 300) { 
     return Promise.resolve(response); 
    } else { 
     var error = {}; 
     error.response = response; 
     error.status = response.status; 
     return Promise.reject(error); 
    } 
}); 
} 

Now when you call the "then" you can write a catch to find your 404: 

fetchPromise.then(function(response) { 
    console.log(response.status); }) 
.catch(function(error) { 
    console.log("problem: ", error.status); 
}); 

저는 이것을 브라우저 콘솔에서만 조롱했습니다. 그래서 이것이 완벽하다고 확신하지 못합니다. 이것이 도움이되는지 또는 해결하기 위해 무엇을했는지 알려 주시기 바랍니다.