【问题标题】:Rails Cucumber Unable to Find Field ErrorRails Cucumber 无法找到字段错误
【发布时间】:2015-11-29 20:29:57
【问题描述】:

这是针对 EdX Berkeley SaaS 课程的。我一次一行地在 Caypbara 场景中挣扎。我现在要做的是访问电影的“编辑”页面并在“导演”字段中填写名称。在尝试这样做时,我得到了错误:

And I fill in "Director" with "Ridley Scott"          # features/step_definitions/web_steps.rb:6
      Unable to find field "Director" (Capybara::ElementNotFound)
      ./features/step_definitions/web_steps.rb:7:in `/^I fill in "(.*?)" with "(.*?)"$/'
      features/search_for_director.feature:19:in `And I fill in "Director" with "Ridley Scott"'

这是一个名为 search_for_directors.feature 的功能文件

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"

现在是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|
  edit_movie_path(arg1)
end

When(/^I fill in "(.*?)" with "(.*?)"$/) do |arg1, arg2|
  fill_in(arg1, :with => arg2)
end

现在当我运行 cucumber 时,我访问编辑页面的步骤通过并且是绿色的:When I go to the edit page for "Alien",所以看起来错误一定与我的视图或控制器有关?但是我已经在所有这些文件中添加了 Director 字段,所以我不确定这里出了什么问题。

这里是edit.html.haml,我认为这应该是这一步唯一相关的视图。

-# edit.html.haml using partial

%h1 Edit Existing Movie

= form_tag movie_path(@movie), :method => :put do

  = label :movie, :title, 'Title'
  = text_field :movie, 'title'

  = label :movie, :rating, 'Rating'
  = select :movie, :rating, ['G','PG','PG-13','R','NC-17']

  = label :movie, :release_date, 'Released On'
  = date_select :movie, :release_date

  = label :movie, :director, 'Director'
  = text_field :movie, 'director'

  = submit_tag 'Update Movie Info'

另外,我更新了控制器以允许在参数中传递 Director 字段:

class MoviesController < ApplicationController

  def movie_params
    params.require(:movie).permit(:title, :rating, :description, :release_date, :director)
  end

【问题讨论】:

  • 这类错误通常是由于您不在您认为的页面上。由于这是一个编辑页面,我猜你需要登录才能这样做,所以你很可能在登录页面上,而不是在电影编辑页面上。尝试将save_and_open_page 添加到您的步骤定义中,它应该会打开当前页面的预览。

标签: ruby-on-rails rspec cucumber capybara


【解决方案1】:

以下是调试此类情况的步骤:

正如文档所说 - “安装 Launchy 后,当 rspec 再次运行时,它将启动特定页面的无样式实例。它在调试集成测试中的错误时特别有用。”

  • 利用 Capybara 方法 save_and_open_page 来验证您的测试是否真的在您认为/应该是的页面上。

  • 如果您在正确的页面上但似乎有其他问题,那么我将使用诸如 Pry 或 Byebug (https://github.com/deivid-rodriguez/byebug) 之类的调试器并将其插入到我的测试中,以允许我仔细看看。

【讨论】:

    【解决方案2】:

    如果没有 Movie#edit 控制器动作代码,很难准确地说出来,但通常在初学者 Rails 示例中,单数动作将一个 ID 号用于被操作的资源(而不是标题)。这通常意味着 edit_movie_path(xxx) 应该采用要编辑的电影的 id 号,而不是电影的标题。如果是这种情况,那么您需要将“我转到...的编辑页面”步骤更改为

    When(/^I go to the edit page for "(.*?)"$/) do |title|
      edit_movie_path(Movie.find_by_title(title).id)
    end
    

    这通常会在 Rails 示例应用程序中将您带到带有输入标题的电影的编辑页面。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 2011-01-15
      • 2020-07-06
      相关资源
      最近更新 更多