2013-06-25 31 views
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 

的,如果中間語句是我的問題,但我需要它採取正則表達式匹配,並決定是否叫「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」追加到它,並稱之爲一種方法。

+0

爲什麼published_name'沒有來電顯示的方法'? –

回答

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 
+0

謝謝,我明白你使用單獨的方法,我可能會。乾杯 – stephenmurdoch

相關問題