问题是: 我有方法
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"}]
}
答案 0 :(得分:2)
解决方案:
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info
{
Title = "ASP.NET Core 2.1+ ConsumerApp API",
Version = "v1"
});
// Set the comments path for the Swagger JSON and UI.
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
c.CustomSchemaIds(i => i.FullName);
});
简化了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
。由于我们可以计算失败测试的数量,因此我们可以将其用于最终计数,而不必再次遍历哈希。
答案 1 :(得分: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。这是一个交互式调试器,可让您逐步执行功能,并查看未按预期进行的事情。