0
votes

Friends I'm in Michael Hartl Ruby Tutorial 9.1.3(Successful edits). After completing instruction when i run

bundle exec rspec spec / 

following error occurs..

 1) User pages edit with valid information 
 Failure/Error: it { should have_link('Sign out',    href: signout_path) }
   expected #has_link?("Sign out", {:href=>"/signout"}) to return true, got false
 # ./spec/requests/user_pages_spec.rb:82:in `block (4 levels) in <top (required)>'

Finished in 2.71 seconds 63 examples, 1 failure, 3 pending

Failed examples:

rspec ./spec/requests/user_pages_spec.rb:82 # User pages edit with valid information

I'm unable to find error I also upload my code where it says error occured. User_pages_spec.rb

User_pages_spec.rb (complete)

  require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "signup page" do
    before { visit signup_path }

    it { should have_content('Sign up') }
    it { should have_title(full_title('Sign up')) }
  end

  describe "signup" 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 "Name",         with: "Example User"
        fill_in "Email",        with: "[email protected]"
        fill_in "Password",     with: "foobar"
        fill_in "Confirmation", with: "foobar"
      end

      describe "after saving the user" do
        before { click_button submit }
        let(:user) { User.find_by(email: '[email protected]') }

        it { should have_link("Sign out") }
        it { should have_title(user.name) }
        it { should have_selector('div.alert.alert-success', text: 'Welcome') }


      end
      it "should create a user" do
        expect { click_button submit }.to change(User, :count).by(1)
   end
   end
  end
describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user) }

    it { should have_content(user.name) }
    it { should have_title(user.name) }
  end

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

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

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

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

    describe "with invalid information" do
      before { click_button "Save changes" }

      it { should have_content('error') }
    end 
  end
  end

end
2
Try some basic debugging. Start by opening the page in a browser. The 'rails console' command will let you run the rspec commands interactively, so you can try and reproduce the issue.Jason
In browser app is working correctly.. but rspec genrated error which i posted aboveJazib Bashir
@Json plz explain some basic debugging. i open Rails console now what?????Jazib Bashir
can you paste more view file ? you must have some more user pagesAbhinay
@Abhinay i edit question and added more pages above.Jazib Bashir

2 Answers

1
votes

Guys i resolved the error which i discuss above. I provide you my new updated code because during search i find that a lot of friends having same problems. so compare your code with my new code and hopefully you error also removed.

user_pages_spec.rb

 require 'spec_helper'

    describe "User pages" do

      subject { page }

      describe "signup page" do
        before { visit signup_path }

        it { should have_content('Sign up') }
        it { should have_title(full_title('Sign up')) }
      end

      describe "signup" 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 "Name",         with: "Example User"
            fill_in "Email",        with: "[email protected]"
            fill_in "Password",     with: "foobar"
            fill_in "Confirmation", with: "foobar"
          end

         describe "after saving the user" do
            before { click_button submit }
            let(:user) { User.find_by(email: '[email protected]') }

            it { should have_link('Sign out') }
            it { should have_title(user.name) }
            it { should have_selector('div.alert.alert-success', text: 'Welcome') }
          end
          it "should create a user" do
            expect { click_button submit }.to change(User, :count).by(1)
       end
       end
      end
    describe "profile page" do
        let(:user) { FactoryGirl.create(:user) }
        before { visit user_path(user) }

        it { should have_content(user.name) }
        it { should have_title(user.name) }
      end

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

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

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


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

         describe "with invalid information" do
          before { click_button "Save changes" }

          it { should have_content('error') }
        end
      end

    end

user_controller.rb

class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
  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)
      flash[:success] = "Profile updated"
      redirect_to @user
    else
      render 'edit'
    end
  end

  def new
    @user = User.new
  end

  private

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

_header.html.erb

<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="navbar-inner">
    <div class="container">
      <%= link_to
<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="navbar-inner">
    <div class="container">
      <%= link_to "sample app", root_path, id: "logo" %>
      <nav>
        <ul class="nav pull-right">
          <li><%= link_to "Home", root_path %></li>
          <li><%= link_to "Help", help_path %></li>
          <% if signed_in? %>
            <li><%= link_to "Users", '#' %></li>
            <li id="fat-menu" class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                Account <b class="caret"></b>
              </a>
              <ul class="dropdown-menu">
                <li><%= link_to "Profile", current_user %></li>
                <li><%= link_to "Settings", edit_user_path(current_user) %></li>
                <li class="divider"></li>
                <li>
                  <%= link_to "Sign out", signout_path, method: "delete" %>
                </li>
              </ul>
            </li>
          <% else %>
            <li><%= link_to "Sign in", signin_path %></li>
          <% end %>
        </ul>
      </nav>
    </div>
  </div>
</header>
 "sample app", root_path, id: "logo" %>
      <nav>
        <ul class="nav pull-right">
          <li><%= link_to "Home", root_path %></li>
          <li><%= link_to "Help", help_path %></li>
          <% if signed_in? %>
            <li><%= link_to "Users", '#' %></li>
            <li id="fat-menu" class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown">
                Account <b class="caret"></b>
              </a>
              <ul class="dropdown-menu">
                <li><%= link_to "Profile", current_user %></li>
                <li><%= link_to "Settings", edit_user_path(current_user) %></li>
                <li class="divider"></li>
                <li>
                  <%= link_to "Sign out", signout_path, method: "delete" %>
                </li>
              </ul>
            </li>
          <% else %>
            <li><%= link_to "Sign in", signin_path %></li>
          <% end %>
        </ul>
      </nav>
    </div>
  </div>
</header>

hope this help! If you need some more details then ask me.

Thanks Jazib Bashir

0
votes

You might find this answer helpful https://stackoverflow.com/a/18806971/2545197

Does your "edit" in user_pages_spec.rb have this part before "describe "with valid information" do" ?

describe "edit" do
    let(:user) { FactoryGirl.create(:user) }
    before do
      sign_in user          # <-this line is needed for spec to look for sign_out
      visit edit_user_path(user)
    end

Also, you should checkout this line from the right after the code in section 9.9 if you have missed.

"Note that Listing 9.9 adds the sign_in method from Listing 9.6 to the before block, which is required for theSign outlink test to pass, and also anticipates protecting the edit action from non-signed-in users (Section 9.2.1)."

So,according to this you should have a piece of code in spec/requests/user_pages_spec.rb like this:

def sign_in(user, options={})
  if options[:no_capybara]
    # Sign in when not using Capybara.
    remember_token = User.new_remember_token
    cookies[:remember_token] = remember_token
    user.update_attribute(:remember_token, User.digest(remember_token))
  else
    visit signin_path
    fill_in "Email",    with: user.email
    fill_in "Password", with: user.password
    click_button "Sign in"
  end
end

If not then please add it. let me know if it helps or not ?