2015-11-29 92 views
0

這是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 
+0

這些錯誤通常是由於您未在您認爲自己的頁面上造成的。由於這是一個編輯頁面,我猜你需要登錄才能這樣做,所以很可能你在登錄頁面上,而不是在電影編輯頁面上。嘗試將'save_and_open_page'添加到您的步驟定義中,它應該打開當前頁面的預覽。 – BroiSatse

回答

0

下面是我想借此調試這類情況的步驟:

正如文檔所說 - 「隨着Launchy的安裝,當rspec再次運行時,它將啓動特定頁面的無風格實例。在集成測試調試錯誤時,它可以是特別有用。」

  • 利用水豚方法save_and_open_page來驗證您在實際測試中的頁面上你是否認爲這是/應該是。

  • 如果你在正確的頁面上,但其他東西似乎是問題,那麼我會利用諸如Pry或Byebug(https://github.com/deivid-rodriguez/byebug)之類的調試器並將其插入到我的測試中,以便讓我仔細觀察。

0

沒有電影#編輯控制器操作代碼很難說清楚,但通常在初學者的軌道例子中,單數動作需要一個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 

那倒通常,在軌例如應用程序,帶你到編輯頁面的電影與輸入的標題。