2014-09-29 124 views
0

我是新來的紅寶石在軌道上。我正在做測試驅動開發。當我運行我的測試,我得到以下錯誤:水豚:: ElementNotFound

1) User Signs Up for Blocitoff Successfully 
    Failure/Error: fill_in 'user_name', with: 'Joe User' 
    Capybara::ElementNotFound: 
     Unable to find field "user_name" 
    # ./spec/features/user_signs_up_spec.rb:6:in `block (2 levels) in <top (required)>' 

這是我的規格:

require 'rails_helper' 

feature 'User Signs Up for Blocitoff' do 
    scenario 'Successfully' do 
     visit new_user_path 
     fill_in 'User name', with: 'Joe User' 
     fill_in 'User email', with: '[email protected]' 
     fill_in 'User password', with: 'joepassword' 
     click_button 'Save' 
    end 
end 

這是我的應用程序/視圖/用戶/ new.html.erb文件:

<%= form_for User.new do |form| %> 
    <%= form.text_field :user_name, placeholder: 'User Name' %> 
    <%= form.text_field :user_email, placeholder: 'User Email' %> 
    <%= form.text_field :user_password, placeholder: 'User Password' %> 
    <%= form.submit 'Save' %> 
<% end %> 

這是users_controller.rb:

class UsersController < ApplicationController 
    def new 
    end 
end 

氏s是我的移民文件:

class CreateUsers < ActiveRecord::Migration 
    def change 
    create_table :users do |t| 
     t.string :user_name 
     t.string :user_email 
     t.string :user_password 

     t.timestamps 
    end 
    end 
end 

我認爲這是某種形式的命名規範,應用程序/視圖/ new.html.erb之間的衝突,以及遷移。任何幫助將不勝感激...

回答

1

fill_in的第一個參數正在尋找一個定位器。如果你檢查你的表單,你會看到該元素的ID可能類似於「user_user_name」。這是因爲您使用的型號是User。如果您的型號爲Cat,那麼ID將爲cat_user_name。嘗試使用

fill_in 'user_user_name', with: 'Joe User' 

與其他人一樣。

+0

工作!非常感謝! – 2014-09-29 17:40:34