Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 然后不扔失败_Javascript_Chai_Cucumberjs - Fatal编程技术网

Javascript 然后不扔失败

Javascript 然后不扔失败,javascript,chai,cucumberjs,Javascript,Chai,Cucumberjs,我正在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 = git

我正在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");
    });
});
}))

现在,当我运行测试时,它一直通过。我甚至试着在那个时候有一个回调,但它仍然在传递,尽管expect抛出了一个异常


我是不是到处都找不到一个训练员。我对这种编程风格完全是新手。

因此,问题可能是for-fetch表示,当出现404时,它不会失败。以下是我的版本:

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); 
});
函数myFetch(url,选项){
if(options==null)options={};
返回fetch(url、选项)。然后返回函数(响应){
如果(response.status>=200&&response.status<300){
回报承诺。解决(回应);
}否则{
var error={};
error.response=响应;
error.status=response.status;
返回承诺。拒绝(错误);
}
});
}
现在,当您调用“then”时,您可以编写一个catch来查找您的404:
fetchPromise.then(函数(响应){
console.log(response.status);})
.catch(函数(错误){
日志(“问题:”,错误。状态);
});
我只是在浏览器控制台上模拟了一下,所以我不确定这是否完美。请告诉我这是否有帮助,或者您是如何解决它的。

不确定“fetch”函数的作用,但假设您的代码按您认为的那样工作,那么您的“then”步骤中缺少一个返回:
return githubResponse。然后…
。如果没有返回,cucumber将在不等待调用执行和完成的情况下结束函数,因此它将始终成功。