我是黄瓜新手,我想问一下如何干掉这段代码(不包含错误):
when /^the user page$/
users_path
when /^the review page$/
reviews_path
我尝试使用像
这样的正则表达式when /^the (.+) page$/
$1.to_s+'s_path'
但显然这是错误的。提前谢谢!
解决方案(基于aledalgrande的答案):
when /^the (.+) page$/
send("#{$1}s_path")
答案 0 :(得分:1)
您可以添加:
特征性/支撑性/ paths.rb
module PathHelpers
def path_to(page_name)
case page_name
when /user/i
users_path
when /review/i
reviews_path
when /home/i
root_path
#add custom here
else
raise "Can't find mapping from \"#{page_name}\" to a path."
end
end
end
World(PathHelpers)
并称之为:
when /^the (.+) page$/ do |page|
visit path_to(page)
end
答案 1 :(得分:1)
这应该有效:
When /^the "(.+)" page$/ do |destination|
send("#{destination}s_path")
end