搜索嵌套的Ruby对象

时间:2018-04-03 11:11:38

标签: ruby

所以我有一个问题我想问我的Ruby对象:

  

我的测试结果中是否有任何失败的情况?

我正在询问的对象如下:

=> #<Newman::Results:0x007fdcc2b48b28 @environment="dev", @build_name="customers", @build_id="119f3875-3793-4ae7-9155-78f0ce8c7047", @scenarios=[#<Newman::Scenario:0x007fdcc2b489c0 @name="POST /customers/zuora/create", @assertions=[#<Newman::Assertion:0x007fdcc2b48970 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b48948 @name="GET ssoauthentication/../Authentication/token", @assertions=[#<Newman::Assertion:0x007fdcc2b488f8 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b488d0 @name="GET /users?CustomerId={customerId}", @assertions=[#<Newman::Assertion:0x007fdcc2b48880 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b48858 @name="GET /locations?CustomerId={customerId}", @assertions=[#<Newman::Assertion:0x007fdcc2b48808 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b487e0 @name="PUT/users/{userId}/avatar", @assertions=[#<Newman::Assertion:0x007fdcc2b48790 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b48768 @name="PUT /customers/{customerId}/logo", @assertions=[#<Newman::Assertion:0x007fdcc2b48718 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b486f0 @name="POST /customers/{customerId}/positions", @assertions=[#<Newman::Assertion:0x007fdcc2b486a0 @test="Successful POST request", @exception_message="expected 200 to be one of [ 201, 202 ]", @exception_source="AssertionError: expected 200 to be one of [ 201, 202 ]\n   at Object.eval sandbox-script.js:1:7)">]>, #<Newman::Scenario:0x007fdcc2b48678 @name="POST /users", @assertions=[#<Newman::Assertion:0x007fdcc2b48628 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b48600 @name="POST /users/{userId}/invitation", @assertions=[#<Newman::Assertion:0x007fdcc2b485b0 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b48588 @name="POST /registration", @assertions=[#<Newman::Assertion:0x007fdcc2b48538 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b48510 @name="PUT /users/{userId}/avatar", @assertions=[#<Newman::Assertion:0x007fdcc2b484c0 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b48498 @name="post to slack", @assertions=[#<Newman::Assertion:0x007fdcc2b48448 @test="Status Code is correct">]>]>

...但要点是,有一个Results对象,其中Scenarios数组中包含一个或多个Scenario个对象。每个Scenario对象都有一个Assertions数组,其中包含一个或多个Assertion个对象,每个Assertion对象包含一个@exception_message属性。除非断言失败,否则此@exception_message属性将为nil,在这种情况下,它将为true

所以,回到问题(&#34;我的测试结果中是否有任何失败的场景?&#34;):在Ruby(而不是Rails)中询问这个问题的最佳方式是什么?

我最好的尝试,results是我的解析结果文件,无论是真还是假,都是我想要的答案:

results.scenarios.to_a.find { |scenario| scenario.assertions.to_a.find { |assertion| assertion.exception_message != nil }}

这似乎有效,但必须是一种更有效的方式。

1 个答案:

答案 0 :(得分:1)

以下是编写上述逻辑的更简洁方法:

results.scenarios.any? do |scenario|
  scenario.assertions.any?(&:exception_message)
end

然而,这仍然是一个非常复杂的逻辑块放在一个地方。你可以通过做类似的事情来进一步打破这个:

results.scenarios.any?(&:failed?)

class Newman::Scenario
  def failed?
    assertions.any?(&:exception_message)
  end
end

甚至,更进一步:

results.scenario_failed?

class Newman::Results
  def scenario_failed?
    scenarios.any?(&:failed?)
  end
end

class Newman::Scenario
  def failed?
    assertions.any?(&:exception_message)
  end
end