干掉路径.rb

时间:2013-03-04 15:13:08

标签: ruby-on-rails cucumber dry

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

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

2 个答案:

答案 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