Rails黄瓜无法找到字段错误

时间:2015-11-29 20:29:57

标签: ruby-on-rails rspec cucumber capybara

这是针对EdX Berkeley SaaS课程的。我一次在一条线上挣扎着Caypbara场景。我现在要做的是访问电影的编辑页面并用名称填写Director字段。在尝试这样做时,我得到错误:

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

现在当我运行黄瓜时,我访问编辑页面的步骤通过并且为绿色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'

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

class MoviesController < ApplicationController

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

2 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

如果没有Movie#edit控制器动作代码,很难准确地说出来,但通常在初学者栏目示例中,单数动作会将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示例应用中,您会转到带有输入标题的电影的编辑页面。