我正在 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");
});
});
});
现在,当我运行测试时,它一直通过。我什至尝试在 Then 中进行回调,但它仍然继续传递,尽管期望抛出异常。
我是否缺少任何处理程序?对于这种编程风格,我完全是新手。
最佳答案
所以问题可能在于 spec for fetch 说当有 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);
});
我只在浏览器控制台中模拟了这一点,所以我不确定这是否完美。请告诉我这是否有帮助或者您做了什么来解决这个问题。
关于javascript - cucumber js然后不抛出失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42721986/