# Can someone please tell me why these test are failing?

**URL:** <https://www.ruby-forum.com/t/can-someone-please-tell-me-why-these-test-are-failing/121149>\
**Category:** Rails\
**Created:** [November 24, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/can-someone-please-tell-me-why-these-test-are-failing/121149 "2007-11-24T16:39:38Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![specpro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@specpro](https://www.ruby-forum.com/u/specpro)\
**Post date:** [November 24, 2007, 4:39pm UTC](https://www.ruby-forum.com/t/can-someone-please-tell-me-why-these-test-are-failing/121149/1 "2007-11-24T16:39:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![specpro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@specpro](https://www.ruby-forum.com/u/specpro)\
**Post date:** [November 24, 2007, 11:51pm UTC](https://www.ruby-forum.com/t/can-someone-please-tell-me-why-these-test-are-failing/121149/2 "2007-11-24T23:51:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![specpro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@specpro](https://www.ruby-forum.com/u/specpro)\
**Post date:** [November 24, 2007, 9:13pm UTC](https://www.ruby-forum.com/t/can-someone-please-tell-me-why-these-test-are-failing/121149/3 "2007-11-24T21:13:11Z")

</div>

On Nov 24, 10:39 am, specpro [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![specpro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@specpro](https://www.ruby-forum.com/u/specpro)\
**Post date:** [November 25, 2007, 4:01am UTC](https://www.ruby-forum.com/t/can-someone-please-tell-me-why-these-test-are-failing/121149/4 "2007-11-25T04:01:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![specpro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@specpro](https://www.ruby-forum.com/u/specpro)\
**Post date:** [November 25, 2007, 5:24pm UTC](https://www.ruby-forum.com/t/can-someone-please-tell-me-why-these-test-are-failing/121149/5 "2007-11-25T17:24:03Z")

</div>

Acuta

---

<div class="post-metadata">

**Author:** ![specpro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@specpro](https://www.ruby-forum.com/u/specpro)\
**Post date:** [November 26, 2007, 3:47pm UTC](https://www.ruby-forum.com/t/can-someone-please-tell-me-why-these-test-are-failing/121149/6 "2007-11-26T15:47:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![specpro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@specpro](https://www.ruby-forum.com/u/specpro)\
**Post date:** [November 26, 2007, 4:41pm UTC](https://www.ruby-forum.com/t/can-someone-please-tell-me-why-these-test-are-failing/121149/7 "2007-11-26T16:41:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![specpro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/50afbb/32.png) [@specpro](https://www.ruby-forum.com/u/specpro)\
**Post date:** [November 25, 2007, 5:22pm UTC](https://www.ruby-forum.com/t/can-someone-please-tell-me-why-these-test-are-failing/121149/8 "2007-11-25T17:22:17Z")

</div>

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
