0

问题是:我有一个方法

def comparison_reporter(list_of_scenarios_results1, list_of_scenarios_results2)
  actual_failed_tests = list_of_scenarios_results2.select {|k,v| v == 'Failed'}
  actual_passed_tests = list_of_scenarios_results2.select {|k,v| v == 'Passed'}
  failed_tests = Array.new(actual_failed_tests.length) { Hash.new }
  failed_tests.each do |hash|
      actual_failed_tests.keys.map {|name| hash["test_name"] = name}
      actual_failed_tests.values.map {|new_status| hash["actual_status"] = new_status}
      list_of_scenarios_results1.values_at(*actual_failed_tests.keys).map {|old_status| hash["previous_status"] = old_status}
  end
  final_result = {
    "passed_tests_count" => list_of_scenarios_results2.select {|k,v| v == 'Passed'}.length,
    "failed_tests_count" => list_of_scenarios_results2.select {|k,v| v == 'Failed'}.length,
    "failed_tests" => failed_tests
  }
  return final_result
end

此方法将 2 个哈希值作为参数,并返回它们的比较结果和其他一些东西。目前,它总是返回failed_tests两个(或更多)相同的哈希值(相同的键值对)。我认为,这个问题是在某个地方failed_tests.each do |hash|,但我找不到这个错误的原因,请指教。方法结果示例(.json 格式)

{
  "passed_tests_count": 3,
  "failed_tests_count": 2,
  "failed_tests": [
    {
      "test_name": "As a user I want to use Recent searches tab",
      "actual_status": "Failed",
      "previous_status": "Failed"
    },
    {
      "test_name": "As a user I want to use Recent searches tab",
      "actual_status": "Failed",
      "previous_status": "Failed"
    }
  ]
}

UPD:hash1(第一个参数)-

{""=>"Passed",
"As a new user I want to use no fee rentals tab"=>"Passed",
"As a new user I want to use Luxury rentals tab"=>"Passed", 
"As a user I want to use Recent searches tab"=>"Failed",
"As a user I want to use new listings for you tab"=>"Passed"}

hash2(第二个参数)-

{""=>"Passed",
"As a new user I want to use no fee rentals tab"=>"Failed",
"As a new user I want to use Luxury rentals tab"=>"Passed",
"As a user I want to use Recent searches tab"=>"Failed",
"As a user I want to use new listings for you tab"=>"Passed"}

所需输出的示例:

{ 
"passed":"count",
"failed":"count",
"failed_tests": [
   {"test_name":"As a user I want to use Recent searches tab",
    "actual_status":"failed",
    "previous_status":"failed"},
   {"test_name":"As a new user I want to use no fee rentals tab",
    "actual_status":"failed",
    "previous_status":"passed"}]
}
4

2 回答 2

2

解决方案:

def comparison_reporter(before, after)
  failed_tests = after.select { |k,v| v == "Failed" }.map do |k,v|
    {
      test_name: k,
      actual_status: v,
      previous_status: before[k]
    }
  end

  {
    passed: after.size - failed_tests.size,
    failed: failed_tests.size,
    failed_tests: failed_tests
  }
end

简化failed_tests了不少。由于我们计算了失败测试的数量,我们可以将它用于最终计数,而不是再次迭代哈希。

于 2018-11-28T15:33:36.890 回答
1

问题出在第 8 行:您在映射时hash["previous_status"]用最后一个值覆盖它。list_of_scenarios_results1.values_at(*actual_failed_tests.keys)

通常,您使用 map 将可迭代对象分配给某些内容,而不是修改其他内容。例如
x = ['1','2','3'].map(&:to_i)
,而不是
x = []; ['1','2','3'].map {|v| x << v.to_i}

我建议重新考虑你的方法。你会在两个哈希中总是有相同的键吗?如果是这样,您可以简化这一点。我还建议调查byebug。它是一个交互式调试器,可让您单步执行您的函数并查看哪些地方没有按照您的预期进行。

于 2018-11-28T15:37:19.867 回答