# Test a login with invalid screen name.
# Giving a Failure
def test_login_failure_with_nonexistent_screen_name
invalid_user = @valid_user
invalid_user.screen_name = "no such user"
try_to_login invalid_user
assert_template "login"
assert_equal "Invalid screen name/password combination",
flash[ :notice]
# Make sure screen_name will be redisplayed, but not the
password.
user = assigns(:user)
assert_equal invalid_user.screen_name, user.screen_name
assert_nil user.password
end
# Test a login with invalid password.
# Giving a Failure
def test_login_failure_with_wrong_password
invalid_user = @valid_user
# Construct an invalid password.
invalid_user.password += "baz"
try_to_login invalid_user
assert_template "login"
assert_equal "Invalid screen name/password combination",
flash[ :notice]
# Make sure screen_name will be redisplayed, but not the
password.
user = assigns(:user)
assert_equal invalid_user.screen_name, user.screen_name
assert_nil user.password
end
def login
@title = "Log in to RailsSpace"
if param_posted?(:user)
@user = User.new(params[:user])
user =
User.find_by_screen_name_and_password(@user.screen_name,
@user.password)
if user
user.login!(session)
flash[:notice] = "User #{user.screen_name} logged in!"
redirect_to_forwarding_url
else
# Don't show the password again in the view.
@user.clear_password!
flash[:notice] = "Invalid screen name/password combination"
end
end
end
Thanks for your help Christopher and Nicholas. Here is the
try_to_login code in the user_controller_test.rb file:
private
# Authorize a user.
def authorize(user)
@request.session[:user_id] = user.id
end
# Try to log a user in using the login action.
def try_to_login(user)
post :login, :user => { :screen_name => user.screen_name,
:password => user.password }
end
I'm wondering if the block if param_posted?(:user) is getting executed
at all. How about moving the following assertions before the flash
message assertion. What do you get when you run the test?
# Make sure screen_name will be redisplayed, but not the
password.
user = assigns(:user)
assert_equal invalid_user.screen_name, user.screen_name
assert_nil user.password
I've moved the block you suggested to before the flash assertion but
still getting the same result:
ruby test/functional/user_controller_test.rb -n /test_login_failure/
Loaded suite test/functional/user_controller_test
Started
FF
Finished in 0.638922 seconds.
1) Failure:
test_login_failure_with_nonexistent_screen_name(UserControllerTest)
[test/functional/user_controller_test.rb:180]:
<"Invalid screen name/password combination"> expected but was
<nil>.
2) Failure:
test_login_failure_with_wrong_password(UserControllerTest) [test/
functional/user_controller_test.rb:196]:
<"Invalid screen name/password combination"> expected but was
<nil>.
Well, I'm stumped - I took your code and quickly setup a test app and
your assertions passed. Are you having problems with other flash
messages? How about changing the flash messages to see if that helps,
perhaps you have a funny character in there. Sorry I couldn't be of
further help.