Louis Morin
Louis Morin

Reputation: 149

Capybara::ElementNotFound

I am new to ruby on rails. I am doing Test Driven Development. When I run my test, I get the following error:

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)>'

This is my spec:

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

This is my app/views/users/new.html.erb file:

<%= 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 %>

This is users_controller.rb:

class UsersController < ApplicationController
    def new
    end
end

This is my migration file:

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

I think it is some kind of naming conflict between the spec, app/views/new.html.erb, and the migration. Any help would be greatly appreciated...

Upvotes: 0

Views: 98

Answers (1)

jeremywoertink
jeremywoertink

Reputation: 2341

The first argument to fill_in is looking for a locator. If you inspect your form, you'll see the ID of that element is probably something like "user_user_name". This is because the model you are using is User. If your model was Cat then then ID would be cat_user_name. Try using

fill_in 'user_user_name', with: 'Joe User'

Same with the rest of them.

Upvotes: 1

Related Questions