如何将一些文本附加到字符串并将其作为方法调用?

时间:2013-06-25 15:39:18

标签: ruby cucumber

如何使下面的黄瓜步骤更漂亮?

Then(/^I should see my (published|unpublished) project$/) do |published_state|

  if published_state == "published"
    project_name = published_name
  elsif published_state == "unpublished"
    project_name = unpublished_name 
  end

  page.should have_content(project_name)
end

中间的if语句对我来说是个问题但我需要它来接受RegEx匹配并决定是否调用“published_name”或“unpublished_name”。我希望能够做到这一点:

Then(/^I should see my (published|unpublished) project$/) do |published_state|    
  page.should have_content(published_state + "_name")
end

我也尝试了以下内容:

page.send should_or_not, have_content("#{pub_state}_name".constantize)
page.send should_or_not, have_content("#{pub_state}_name".to_sym)

基本上我想把一个字符串“发布”,并在其上添加“_name”,并将其称为方法。

1 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

Then(/^I should see my (published|unpublished) project$/) do |published_state|
  page.should have_content(send(:"#{published_state}_name"))
end

但是,我只想写:

Then /^I should see my published project$/ do
  page.should have_content(published_name)
end

Then /^I should see my unpublished project$/ do
  page.should have_content(unpublished_name)
end