2014-09-29 160 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

相关问题