0

我怎样才能让下面的黄瓜步骤更漂亮?

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”附加到它,并将其作为方法调用。

4

1 回答 1

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
于 2013-06-25T16:23:33.883 回答