简体   繁体   中英

signed_in? function not working in Chapter 8-9 of Rails Tutorial (Hartl)

I have one rspec test failing.

  1) User pages edit with valid information should have link "Sign out", {:href=>"/signout"}
 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:80:in `block (4 levels) in <top (required)>'

The test looks to see if a 'Sign out' link is present in the header after signing in. The code works in a browser but not in rspec. The spec in question is aligned with the Hartl tutorial:

describe "User pages" do

  subject { page }

  ...

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

    ...

    describe "with valid information" do 
        let(:new_name)  { "New Name" }
        let(:new_email) { "new@example.com"}
        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
  end
end

The following code is the header template being tested (also follows the Hartl Rails tutorial). I've run some tests (using puts ) and found out that after logging in, the signed_in? function is returning false to rspec (while returning true when tested in the browser).

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

It appears this is a problem with rspec and I haven't been able to figure out what to do to remedy.

Thanks in advance for your help.

Update : The original version of this answer focused on Listing 9.1, which turned out to have been correct as is and has since been reverted to its current state with no sign_in user due to other problems the removal caused . However, the issue with Listing 9.9 noted in the OP's summary comment was a bona fide problem and the fix to add sign_in user to that listing remains.

This does appear to have been a bug in the Rails 4 tutorial , which dropped the sign_in(user) line from the before block in listing 9.9 that was present in the previous version. It has since been corrected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM