我的问题涉及Ruby的find
方法以及为什么它在一种情况下工作而在另一种情况下不工作。注意:我在用Ruby编写的Cucumber步骤定义中使用此方法。
此代码块按我希望的方式工作:
Then(/^I expect the bolt service children for CrossrefDepositsDaf to include only: IngestUpdateWorkflow$/) do
@boltworkflowservice ||= BoltWorkflowService.new
Watir::Wait.until(60) do
@job_family = @boltworkflowservice.get_workflows_job_family(@job_id).json
@job_family.find { |job| job['type'] == "IngestUpdateWorkflow" }
end
expect(@job_family.find{|job| job['type'] == "IngestUpdateWorkflow"}.nil?).to eq(false), "IngestUpdateWorkflow child not found"
end
但是我想重写这个代码块来摆脱实例变量并将块减少一行。我尝试了以下方法:
Then(/^I expect the bolt service children for CrossrefDepositsDaf to include only: IngestUpdateWorkflow$/) do
@boltworkflowservice ||= BoltWorkflowService.new
Watir::Wait.until(60) do
puts @boltworkflowservice.get_workflows_job_family(@job_id).json
@boltworkflowservice.get_workflows_job_family(@job_id).json.find {|job| job['type'] == "IngestUpdateWorkflow"}
end
expect(@job_family.find{|job| job['type'] == "IngestUpdateWorkflow"}.nil?).to eq(false), "IngestUpdateWorkflow child not found"
end
我刚添加puts
以查看代码将返回的内容。根据{{1}},它返回一个包含三个哈希对象的数组,但我仍然收到错误。这是我尝试运行第二个块时得到的结果:
puts
有人可以向我解释为什么第二段代码不起作用? [{"id"=>914295, "type"=>"CrossrefDepositsDaf", "subType"=>"", "parentId"=>0, "state"=>"SUCCESS", "subState"=>"SUCCESS", "dataPointer"=>"s3://sequoia-install/app-resources/test.cirrostratus.org/workflows/CrossrefDepositsDaf/2015/7/29/148c7ebc-3da4-4650-a22e-2e8962d448fc", "created"=>"2015-07-29 01:58:14", "metadata"=>[]}, {"id"=>914296, "type"=>"CrossRefDeposits", "subType"=>"BoltCatDaf", "parentId"=>914295, "state"=>"SUCCESS", "subState"=>"SUCCESS", "dataPointer"=>"s3://sequoia-install/app-resources/test.cirrostratus.org/workflows/CrossRefDeposits/2015/7/29/3a703736-d4a2-456e-baa9-7bf2c0f4a0e2", "created"=>"2015-07-29 01:58:17", "metadata"=>[]}, {"id"=>914297, "type"=>"IngestUpdateWorkflow", "subType"=>"CrossRefDeposits", "parentId"=>914296, "state"=>"SUCCESS", "subState"=>"CREATED", "dataPointer"=>"s3://sequoia-install/app-resources/test.cirrostratus.org/workflows/IngestUpdateWorkflow/2015/7/29/9282591b-f93c-49d6-b656-4afbd14156ef", "created"=>"2015-07-29 01:58:23", "metadata"=>[]}]
NoMethodError: undefined method `find' for nil:NilClass
./features/step_definitions/catdaf_integration_crossref_deposits_steps.rb:42:in `/^I expect the bolt service children for CrossrefDepositsDaf to include only: IngestUpdateWorkflow$/'
C:\Users\Dugan23\workspace\cloudy_clouds\cucumber\features\content_management\daf\integration\crossref_deposits.feature:11:in `And I expect the bolt service children for CrossrefDepositsDaf to include only: IngestUpdateWorkflow'
1 scenario (1 failed)
6 steps (1 failed, 5 passed)
9m30.221s
Process finished with exit code 1
表明我应该为puts
方法提供一个数组,为什么它会说我试图在find
上运行find
方法。提前感谢您提供的任何和所有帮助。
注意:当我向第一个代码块添加nil:NilClass
语句以打印出puts
时,它返回与第二个代码块完全相同的数组。这是否意味着在第二个代码块中,Ruby在评估代码之前尝试运行@job_family
方法?我尝试通过如下添加括号来解决这个问题,但无济于事:
find
答案 0 :(得分:0)
在第二个版本中,你避免在块中使用@job_family,但你仍然指的是期望值,除非你在其他地方定义了它,否则显然是零