1

我是黄瓜的新手,我想问一下如何将这段代码变干(不包含错误):

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")
4

2 回答 2

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
于 2013-03-04T15:27:57.950 回答
1

这应该有效:

When /^the "(.+)" page$/ do |destination|
  send("#{destination}s_path")
end
于 2013-03-05T15:20:37.693 回答