RoR Tutorial Chapter 9 Test Failing
I have been going through MHartl's tutorial and I have 5 persistent errors chapter 9, all relating to LayoutLinks. I have checked and rechecked and I do not see where my code differs from his, but running his code on my computer passes all of the tests.
I have pasted an example of the error below, highlighting that it seems to be showing the root page (via "visit root_path") without being signed in even after going through the "factory(:user) -> fill_in info -> click_button" set of instructions. So somewhere in there, I think that I am either just losing the logged_in status or not registering at all.
Any insight? Thanks very much. This is quite confounding.
This is an example of the error:
LayoutLinks when signed in should have a signout link
开发者_如何学编程 Failure/Error: response.should have_selector("a", :href => signout_path,
expected following output to contain a <a href='/signout'>Sign Out</a> tag:
<!DOCTYPE html>
<html>
<head>
...
...
<li><a href="/">Home</a></li>
<li><a href="/help">Help</a></li>
<li><a href="/signin">Sign in</a></li>
</ul></nav></header><!--
<header>
<nav class="round">
<ul>
<li><a href="/">Home</a></li>
<li><a href="/help">Help</a></li>
<li><a href="/signin">Sign In</a></li>
</ul>
</nav>
...
I had the same problem and realized the Factory(:user) call wasn't persisting the user to the database, I ended up using the following to make it work:
@user = Factory(:user)
visit signup_path
fill_in :email, :with => @user.email
fill_in :name, :with => @user.name
fill_in :password, :with => @user.password
fill_in "Confirmation", :with => @user.password_confirmation
click_button
visit signin_path
fill_in :email, :with => @user.email
fill_in :password, :with => @user.password
click_button
Note that you will have to also add the following to clear the db after the test runs
after(:each) do
User.find_by_email(@user.email).destroy
end
Is it not simple? You have "Sign In" there, but not signout path :) Meaning, maybe you should add
<li><a href="/signout">Sign Out</a></li>
to your view.
It looks like the test isn't signing the user in correctly. You can compare your code to the completed sample app to see if that helps you solve the problem.
I'm seeing the same problem, and I have put in logs to check what is happening. In SessionsHelper>>sign_in I can see that @current_user is being set correctly. Later, when we display the user page, it calls SessionsHelper>>signed_in? but now @current_user is nil, so I don't display the 'signed out' and 'profile' links, and two of the tests fail.
What seems to be happening is that when we sign in we are doing so in an instance of SessionsController, which calls the sign_in method in the included module, which in turn sets the @current_user in that instance of SessionsController. Then it redirects to user, which passes control to the UsersController, and the code in _header.html.erb which calls signed_in? is executed in the context of the UsersController. However, this controller has its own copy of the @current_user variable, which is not effected by running sign_in in the SessionsController, so it returns nil.
Same thing happened to me as was happening to Alun ap Rhisiart
My problem was for the second line in this block...
def sign_in(user)
cookies.permanent.signed[:remember_token] = [user.id, user.salt]
self.current_user = user
logger.debug "sign_in(user) - #{self.current_user}"
end
...I had accidentally put [user.id], [user.salt]
Doing the chapter 9 of the Tutorial I had the same problem. The cause turned out to be my own inaccuracy in typing the code.
I figured out (by manipulating _header.html.erb) that signed_in?
method defined in app/helpers/session_helper.rb
worked incorrectly and all due to an extra @ .
I had this (using instance variable) :
def signed_in?
! @current_user.nil?
end
But should have used the method current_user
instead:
def signed_in?
! current_user.nil?
end
Found it in my case, might be worth checking for yours. I was never actually signing in properly because in User.rb self.authenticate_with_salt my first line was
user = find_by_id(:id)
instead of
user = find_by_id(id)
with that taken care of the tests pass and I get the signout etc links.
精彩评论