Rails黄瓜路径映射错误

Rails Cucumber Path Mapping Error

我是 Cucumber 和 Capybara 的新手,我很难理解如何正确设置路径。 运行 当我的黄瓜测试未找到 "Alien" 的编辑页面的映射时,我收到此错误...但我定义了 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()[: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())

    else
      begin
        page_name =~ /^the (.*) page$/
        path_components = .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 即可进行测试,我得到了我在 post 顶部提供的错误。所以在这一点上我想我有两个问题。为什么这个 happening/how 是我的 edit_movies_path 定义不正确?其次,为什么我什至需要指定它?如果我 运行 rake routes,我可以看到所有常用的路径别名,这似乎与 "convention over configuration" 的哲学相矛盾,必须指定我已经指定的内容我的路线文件。

好的,这是:

注意你的web_steps

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

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

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()[:id])

    else
      begin
        page_name =~ /^the (.*) page$/
        path_components = .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_name 中的大小写与您提供的 edit_movie_path(arg1) 不匹配,因此它将回退到:

   else
      begin
        page_name =~ /^the (.*) page$/
        path_components = .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()[:id])

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

这样的表达式

Note, change this to include a carat at the front like: when /^the edit page for "(.*)"$/ The carat means that is the beginning of the expression.

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

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

希望这足以说明...