【问题标题】:Capybara::ElementNotFound水豚::ElementNotFound
【发布时间】:2014-09-30 01:29:12
【问题描述】:

我是 ruby​​ on rails 的新手。我正在做测试驱动开发。当我运行测试时,我收到以下错误:

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: 'joeuser@example.com'
        fill_in 'User password', with: 'joepassword'
        click_button 'Save'
    end
end

这是我的 app/views/users/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

这是我的迁移文件:

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

我认为这是规范、app/views/new.html.erb 和迁移之间的某种命名冲突。任何帮助将不胜感激...

【问题讨论】:

    标签: ruby-on-rails rspec capybara


    【解决方案1】:

    fill_in 的第一个参数是寻找定位器。如果您检查表单,您会看到该元素的 ID 可能类似于“user_user_name”。这是因为您使用的模型是User。如果您的模型是 Cat,那么 ID 将是 cat_user_name。尝试使用

    fill_in 'user_user_name', with: 'Joe User'
    

    与其他人相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      相关资源
      最近更新 更多