2017-10-25 4 views
0

내가이 문제를 해결하기 위해 노력하고있어 ElementNotFound,하지만 난 말 Cabybara에서 오류 메시지를 수신 :레일 : 카피 바라가 ::

`Failure/Error: fill_in 'Name', with: 'Vostro 2017' 
    Capybara::ElementNotFound: Unable to find visible field "Name" that is not disabled` 

new.html.erb입니다 :

<%= form_for @item, url: {action: "create"} do |f|%> 
    <%= f.label 'Name' %> 
    <%= f.text_field :name %> 
    <%= f.label 'Description' %> 
    <%= f.text_field :description %> 
    <%= f.label 'Features' %> 
    <%= f.text_field :features %> 
    <%= f.label 'Asset number' %> 
    <%= f.text_field :assetNumber %> 
    <%= f.submit%> 
<% end %> 

그리고 내 item_controller.rb입니다 :

class ItemController < ApplicationController 
    def show 
    @items = Item.find(params[:id]) 
    end 
    def new 
    @item = Item.new 
    end 

    def create 
    @item = Item.new(item_params) 

    @item.save 
    redirect_to @item 
    end 

    private 
    def item_params 
    params.require(:item).permit(:name, :description, :features, :assetNumber) 
    end 
end 

테스트를 수행하는 데 사용되는 RSpec에 파일은 다음과 같습니다

require 'rails_helper' 

feature 'User creates a new inventory item' do 
    scenario 'successfully' do 
    visit new_item_path 
    fill_in 'Name', with: 'Vostro 2017' 
    fill_in 'Description', with: 'Dell Notebook' 
    fill_in 'Features', with: '16gb, 1Tb, 15.6"' 
    fill_in 'Asset number', with: '392 DLL' 
    click_button 'Create Item' 

    expect(page).to have_content 'Vostro 2017' 
    expect(page).to have_content 'Dell Notebook' 
    expect(page).to have_content '16gb, 1Tb, 15.6"' 
    expect(page).to have_content '392 DLL' 
    end 
end 

저는 ruby-2.3.5 및 rails 4.1.0을 사용하고 있습니다. 저는 Ruby/Rails에서 초보자이기 때문에 코드에 어떤 문제가 있는지 알 수 없습니다. 누군가 나를 해결할 수 있도록 도와 줄 수 있습니까? 미리 감사드립니다.

find("input[id$='name']").set "Vostro 2017" 

나 :

+0

모달과 함께 pls로 질문을 업데이트 할 수 있습니까? –

+0

@Ziyan Junaideen 모달이란 무엇입니까? – viniCdaSilva

답변

0
당신은이 작업을 수행 할 수

는, 귀하의 의견을 가정하는 것은 name의 ID가

find("#name").set "Vostro 2017" 

또한 시도 할 수 있습니다를 Name을 낮은 케이싱 :

fill_in 'name', with: "Vostro 2017" 

Capybara는 이름 또는 id 속성을 대상으로하므로 두 번째 예제가 작동합니다.

+0

테스트 파일을 변경하면 챌린지 소유자가 마음에 들지 않습니다. – viniCdaSilva

0

레일즈는 양식 객체를 사용하여 양식 입력 이름을 생성합니다.

fill_in 'item[name]', with: "Vostro 2017" 
fill_in 'item[description]', with: 'Dell Notebook' 
fill_in 'item[features]', with: '16gb, 1Tb, 15.6"' 
fill_in 'item[assetNumber]', with: '392 DLL' 
0

해당 페이지 대신 ERB 템플릿 당신은 당신의 레이블이 사실과 연관되지 않은 알 수 있습니다 (귀하의 질문은 ERB에 대해 특별히하지 않는 한 항상 더 나은는 HTML을 포함)의 실제 HTML을 보면 입력 요소 (입력 ID에 일치하는 for 속성 없음). 분명히 카피 바라 (Capybara)가 라벨 텍스트 (귀하의 경우 '이름')로 요소를 찾으려면 라벨을 요소와 올바르게 연관시켜야합니다. 문제를 해결하려면 f.label - http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-label을 올바르게 사용해야합니다. 당신은 내가 잘못하고 있었는지 깨달았다

f.label :name, 'Name' 
+0

멋진 팁. 고마워. – viniCdaSilva

0

것 (국제화 번역에서 추출 된 텍스트를 사용하여 대) 요소의 텍스트를 지정, 그래서 수 있도록하려는 경우의 갈 : 나는 데프 내부 인스턴스 변수 항목을 변경 item (복수형이 아님)에 액션을 표시하고 assetNumber에서 asset_number로 속성을 변경 했으므로 Cabybara 테스트에서 올바르게 이해할 수 있습니다.

감사합니다.