Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.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_Node.js_Reflection_Mocha.js_Async.js - Fatal编程技术网

在javascript中使用反射在失败时获取测试函数的名称

在javascript中使用反射在失败时获取测试函数的名称,javascript,node.js,reflection,mocha.js,async.js,Javascript,Node.js,Reflection,Mocha.js,Async.js,我们如何确定被调用的变量函数的名称 我有一个函数作为测试实用程序函数来设置我的测试用例。根据测试传递给测试实用程序函数的参数,某些子函数可能会执行,也可能不会执行。有时这些子函数会失败,我需要知道哪一个失败了,但不知道如何做到这一点。在其他语言中,反射是我在这里使用的。这是一种选择吗 下面是我们正在做的一个抽象示例: exports.test_util_func = function(params, callback){ //initialize the functions var a =

我们如何确定被调用的变量函数的名称

我有一个函数作为测试实用程序函数来设置我的测试用例。根据测试传递给测试实用程序函数的参数,某些子函数可能会执行,也可能不会执行。有时这些子函数会失败,我需要知道哪一个失败了,但不知道如何做到这一点。在其他语言中,反射是我在这里使用的。这是一种选择吗

下面是我们正在做的一个抽象示例:

exports.test_util_func = function(params, callback){
 //initialize the functions
 var a = function(callback){
  ...
  //response might be "{status:400, body: {error: {...} } }"
  callback(error, response);
 }
 var b = function(callback){
  ...
  callback(error, response);
 }
 //determine what functions to run
 var functions_to_run = [];
 if(params.a) functions_to_run.push(a);
 if(params.b) functions_to_run.push(a);
 async.series(functions, function(error, responses){
  if(error) throw new Error(error);
  for(var i in responses){(function(response){
    //verify that we received a 200 success status from the server
    //if we didn't, capture the error
    if(response.status!==200){
      console.log(response.body);
      console.log(i);
      //HELP NEEDED HERE - how do we capture better than the function iteration so we know what actually failed? Ideally, we would have: "reflective call()
      throw new Error(i+": "+JSON.stringify(response.body));
    }
  })(responses[i]);}
 })
}

编辑:下面这篇文章中可能有一些我们可以使用的东西,但我想一定有更简单的方法可以使用_prototype info:

我找到了一种解决方法,可以让我继续使用这种模式,但并不完全可靠。创建函数后,我们创建并使用将名称存储在函数的原型中

理想的情况仍然是能够引用变量本身的名称

最终代码示例:

exports.test_util_func = function(params, callback){
 //initialize the functions
 var a = function(callback){
  ...
  //response might be "{status:400, body: {error: {...} } }"
  callback(error, response);
 }
 a.prototype.name = "a";
 var b = function(callback){
  ...
  callback(error, response);
 }
 a.prototype.name = "b";
 //determine what functions to run
 var functions_to_run = [];
 if(params.a) functions_to_run.push(a);
 if(params.b) functions_to_run.push(a);
 async.series(functions, function(error, responses){
  if(error) throw new Error(error);
  for(var i in responses){(function(response){
    //verify that we received a 200 success status from the server
    //if we didn't, capture the error
    if(response.status!==200){
      console.log(functions.map(function(func){return func.prototype.name})[i]);
      console.log(response.body);
    }
  })(responses[i]);}
 })
}