【发布时间】: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