Rails黄瓜路径映射错误

时间:2015-11-29 07:40:59

标签: ruby-on-rails cucumber capybara

我是Cucumber和Capybara的新手,我很难理解如何以正确的方式设置路径。我在运行黄瓜测试时遇到这个错误,因为没有找到“异形”编辑页面的映射....但我定义了edit_movie_path,所以我看不到它被绊倒的地方。

这是一个名为search_for_directors.featur

的功能文件
Feature: Search for movies by director

  As a movie buff
  So that I can find movies with my favorite director
  I want to include and serach on director information in movies I enter

Background: movies in database

  Given the following movies exist:
  | title        | rating | director     | release_date |
  | Star Wars    | PG     | George Lucas |   1977-05-25 |
  | Blade Runner | PG     | Ridley Scott |   1982-06-25 |
  | Alien        | R      |              |   1979-05-25 |
  | THX-1138     | R      | George Lucas |   1971-03-11 |

Scenario: add director to existing movie
  When I go to the edit page for "Alien"
  And  I fill in "Director" with "Ridley Scott"
  And  I press "Update Movie Info"
  Then the director of "Alien" should be "Ridley Scott"

Scenario: find movie with same director
  Given I am on the details page for "Star Wars"
  When  I follow "Find Movies With Same Director"
  Then  I should be on the Similar Movies page for "Star Wars"
  And   I should see "THX-1138"
  But   I should not see "Blade Runner"

Scenario: can't find similar movies if we don't know director (sad path)
  Given I am on the details page for "Alien"
  Then  I should not see "Ridley Scott"
  When  I follow "Find Movies With Same Director"
  Then  I should be on the home page
  And   I should see "'Alien' has no director info"

现在这里是movie_steps.rb

Given(/^the following movies exist:$/) do |table|
  table.hashes.each do |movie|
  # table is a Cucumber::Ast::Table
    Movie.create(movie)
  end
end

这是web_steps.rb

When(/^I go to the edit page for "(.*?)"$/) do |arg1|
  debugger
  visit path_to(edit_movie_path(arg1))
end

最后,这是paths.rb

module NavigationHelpers
  # Maps a name to a path. Used by the
  #
  #   When /^I go to (.+)$/ do |page_name|
  #
  # step definition in web_steps.rb
  #
  def path_to(page_name)
    case page_name

    when /^the (RottenPotatoes )?home\s?page$/ then '/movies'
    when /^the movies page$/ then '/movies'

    when /the edit page for "(.*)"$/
      edit_movie_path(Movie.find_by_title($1)[:id])

    # Add more mappings here.
    # Here is an example that pulls values out of the Regexp:
    #
    #   when /^(.*)'s profile page$/i
    #     user_profile_path(User.find_by_login($1))

    else
      begin
        page_name =~ /^the (.*) page$/
        path_components = $1.split(/\s+/)
        self.send(path_components.push('path').join('_').to_sym)
      rescue NoMethodError, ArgumentError
        raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
          "Now, go and add a mapping in #{__FILE__}"
      end
    end
  end
end

World(NavigationHelpers)

我只需键入cucumber即可运行测试,我收到了帖子顶部提供的错误。所以在这一点上我想我有两个问题。为什么会发生这种情况/我的edit_movies_path定义如何不正确?其次,为什么我甚至需要指定这个?如果我运行rake routes,我可以看到所有常用的路径别名,它似乎与“约定优于配置”的哲学相矛盾,必须指定我已在路由文件中指定的内容。

1 个答案:

答案 0 :(得分:1)

好的,这是:

注意您的web_steps

When(/^I go to the edit page for "(.*?)"$/) do |arg1|
  debugger
  visit path_to(edit_movie_path(arg1))
end

您已向edit_movie_path(arg1)提供path_to,其路径如下:

def path_to(page_name)
    case page_name

    when /^the (RottenPotatoes )?home\s?page$/ then '/movies'
    when /^the movies page$/ then '/movies'

    when /the edit page for "(.*)"$/
      edit_movie_path(Movie.find_by_title($1)[:id])

    else
      begin
        page_name =~ /^the (.*) page$/
        path_components = $1.split(/\s+/)
        self.send(path_components.push('path').join('_').to_sym)
      rescue NoMethodError, ArgumentError
        raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
          "Now, go and add a mapping in #{__FILE__}"
      end
    end
  end
end

您所提供的case page_nameedit_movie_path(arg1)匹配的情况都没有,因此它会回归到:

   else
      begin
        page_name =~ /^the (.*) page$/
        path_components = $1.split(/\s+/)
        self.send(path_components.push('path').join('_').to_sym)
      rescue NoMethodError, ArgumentError
        raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
          "Now, go and add a mapping in #{__FILE__}"
      end
    end

部分,分裂,打破并重新加入你所看到的道路。

要解决这个问题,可以采取以下措施:

请注意,您有以下案例:

when /the edit page for "(.*)"$/
  edit_movie_path(Movie.find_by_title($1)[:id])

这是您可以用来访问编辑页面的内容。这是寻找像the edit page for...

这样的表达式
  

注意,将其更改为在前面包含一个克拉,如:when /^the edit page for "(.*)"$/克拉表示这是表达式的开头。

因此,在您的web_steps中,将代码重构为以下内容:

When(/^I go to the edit page for "(.*?)"$/) do |arg1|
  debugger
  the edit page for arg1
end

希望这足够解释......