0
votes

I'm back again with yet another question. I'm working through the Michael Hartl rails tutorial, and I seem to get stuck at this part (I've redone it three times with version control, and I get the same results here.)

I'm getting this error message:

2) User pages edit with valid information Failure/Error: fill_in "First", with: new_first Capybara::ElementNotFound: Unable to find field "First" # ./spec/requests/user_pages_spec.rb:57:in `block (4 levels) in '

And I get that same message multiple times. I think I have about 7 errors with that as the cause at this point.

So, here's the relevant code:

require 'spec_helper'

describe "User pages" do

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user) }
  end

  describe "signup page" do

    before { visit signup_path }

    let(:submit) { "Create my account" }

    describe "with invalid information" do
      it "should not create a user" do
        expect { click_button submit }.not_to change(User, :count)
      end
    end

    describe "with valid information" do
      before do
        fill_in "First",        with: "Example"
        fill_in "Last",         with: "User"
        fill_in "Email",        with: "[email protected]"
        fill_in "Password",     with: "foobar"
        fill_in "Confirmation", with: "foobar"
        expect do
          click_button "Create my account"
        end.to change(User, :count).by(1)
      end
    end
  end

  describe "edit" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit edit_user_path(user) }

    describe "page" do

      specify { page { should have_content("Update your profile") } }
      specify { page { should have_title("Edit User") } }
      specify { page { should have_link('change', href: 'http://gravatar.com/emails') } }
    end

    describe "with invalid information" do
      before { click_button "Save changes" }
      specify { page { should have_content("error") } }
    end

    describe "with valid information" do
      let(:new_first)  { "First" }
      let(:new_last)  { "Last" }
      let(:new_email) { "[email protected]" }
      before do
        fill_in "First",             with: new_first
        fill_in "Last",             with: new_last
        fill_in "Email",            with: new_email
        fill_in "Password",         with: user.password
        fill_in "Confirm Password", with: user.password
        click_button "Save changes"
      end

      specify { page { should have_selector("div.alert.alert-success") } }
      specify { page { should have_title("new_first") } }
      specify { page { should have_link('Sign out', href: signout_path) } }
      specify { expect(user.reload.name).to  eq new_first }
      specify { expect(user.reload.name).to  eq new_last }
      specify { expect(user.reload.email).to eq new_email }
    end
  end
end

That was my user_pages spec. This is my user controller.

class UsersController < ApplicationController
  before_action :signed_in_user, only: [:edit, :update]
    def show
    @user = User.find(params[:id])
  end

  def new
    @user = User.new
  end

  def create
    @user = User.new(user_params)
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      render 'new'
    end
  end

  def edit
    @user = User.find(params[:id])
  end

  def update
    @user = User.find(params[:id])
    if @user.update_attributes(user_params)
      # Handle a successful update.
    else
      render 'edit'
    end
  end

  private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
    end

    # Before filters

    def signed_in_user
      redirect_to signin_url, notice: "Please sign in." unless signed_in?
    end
end

And here's the html page itself.

<% provide(:title, "Edit user") %>
<h1>Update your profile</h1>

<div class="row">
  <div class="span6 offset3">
    <%= form_for(@user) do |f| %>
      <%= render 'shared/error_messages' %>

      <%= f.label :first %>
      <%= f.text_field :first %>

      <%= f.label :last %>
      <%= f.text_field :last %>

      <%= f.label :email %>
      <%= f.text_field :email %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.label :password_confirmation, "Confirm Password" %>
      <%= f.password_field :password_confirmation %>

      <%= f.submit "Save changes", class: "btn btn-large btn-primary" %>
    <% end %>

    <%= gravatar_for @user %>
    <a href="http://gravatar.com/emails">change</a>
  </div>
</div>
1

1 Answers

0
votes

I had the same error with the password field. I added "user_" to the field with the error to fix it .

describe "with valid information" do
    before do
     fill_in "Name",           with: "Example User"
     fill_in "Email",          with: "[email protected]"
     fill_in "user_password",  with: "foobar"
     fill_in "Confirmation",   with: "foobar"
end