Cucumber:用于在 ruby 中完成方法的变量

Cucumber: variable used to complete a method in ruby

提问人:Kamilo 提问时间:7/21/2023 最后编辑:Kamilo 更新时间:7/22/2023 访问量:25

问:

有没有一种优雅的方式在下面的方法中使用变量 - 因此,无论在步骤中用作第一个字符串的名称,都将反映在 ?var_namevar

Then('the {string} variable in the response should be {string}') do |var_name, value|
  var = Connection.session.**"#{var_name}"**
  raise "Expected #{var} to equal '#{value}', but was #{var}" unless var == value 
end

奥拉 卡米洛

Ruby 测试 方法 Cucumber 变量赋值

评论


答:

1赞 Jörg W Mittag 7/22/2023 #1

为了使用只有在运行时才知道的选择器发送消息,可以使用 Object#public_send 方法。 允许您通过仅将消息名称作为参数传递来向对象发送任何消息:public_send

Connection.session.public_send(var_name)