Can someone please tell me why these test are failing?

  1. Failure:
    test_login_failure_with_nonexistent_screen_name(UserControllerTest) [./
    test/functional/user_controller_test.rb:140]:
    <“Invalid screen name/password combination”> expected but was
    .

  2. Failure:
    test_login_failure_with_wrong_password(UserControllerTest) [./test/
    functional/user_controller_test.rb:155]:
    <“Invalid screen name/password combination”> expected but was
    .

---------------UserControllerTest-----------------------

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

-----------------------UserController---------------------------

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

There is a couple of things here that would help to know to solve your
issue:

invalid_user = @valid_user
invalid_user.screen_name = “no such user”
try_to_login invalid_user

  • try_to_login: is this a helper method that actually does a post?
  • if so, it looks like you are passing a model rather than a hash of
    parameters

In other words, I would expect to be seeing something like this:

post :login, ‘user’ => {‘screen_name’ => ‘no such user’, ‘password’ =>
‘testing’}

maybe show us the try_to_login method may enable us to help you.

On Nov 24, 10:39 am, specpro [email protected] wrote:

try_to_login invalid_user

Is it possible your try_to_login method isn’t doing a post? Because
the test and code you’ve provided look fine.

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

Acuta

Hi Nicholas,

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
    .

  2. Failure:
    test_login_failure_with_wrong_password(UserControllerTest) [test/
    functional/user_controller_test.rb:196]:
    <“Invalid screen name/password combination”> expected but was
    .

2 tests, 8 assertions, 2 failures, 0 errors

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.

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