這是EdX Berkeley SaaS類。我每次都在一條線上艱難地度過Caypbara場景。我現在想要做的是訪問電影的編輯頁面,並用名稱填寫「導演」字段。在試圖這樣做,我得到的錯誤:Rails Cucumber無法找到字段錯誤
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傳遞:
class MoviesController < ApplicationController
def movie_params
params.require(:movie).permit(:title, :rating, :description, :release_date, :director)
end
這些錯誤通常是由於您未在您認爲自己的頁面上造成的。由於這是一個編輯頁面,我猜你需要登錄才能這樣做,所以很可能你在登錄頁面上,而不是在電影編輯頁面上。嘗試將'save_and_open_page'添加到您的步驟定義中,它應該打開當前頁面的預覽。 – BroiSatse