# Testing question: try to login

**URL:** <https://www.ruby-forum.com/t/testing-question-try-to-login/192476>\
**Category:** Rails\
**Created:** [July 30, 2010, 9:48pm UTC](https://www.ruby-forum.com/t/testing-question-try-to-login/192476 "2010-07-30T21:48:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![noloman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a9a28c/32.png) [@noloman](https://www.ruby-forum.com/u/noloman)\
**Post date:** [July 30, 2010, 9:48pm UTC](https://www.ruby-forum.com/t/testing-question-try-to-login/192476/1 "2010-07-30T21:48:57Z")

</div>

Hey everybody,  
I’m trying to test some code I wrote, and I wrote the following

def try\_to\_login(antonio)  
antonio = users(:one)  
#post :login, :user =\> { :screen\_name =\> user.screen\_name, :email =\>  
user.email, :password =\> user.password }  
#post :login, :screen\_name =\> antonio.screen\_name, :email =\>  
antonio.email, :password =\> antonio.password  
assert logged\_in?  
end

If I uncomment the first post line, I get "undefined local method or  
variable ‘user’ for UserControllerTest, even though I have a model  
called User.rb with a table called users in my database.  
However, if I comment that line and uncomment the second post line,  
everything works like a charm… could anyone tell me why?

Thanks everybody in advance!

---

<div class="post-metadata">

**Author:** ![noloman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a9a28c/32.png) [@noloman](https://www.ruby-forum.com/u/noloman)\
**Post date:** [July 30, 2010, 10:00pm UTC](https://www.ruby-forum.com/t/testing-question-try-to-login/192476/2 "2010-07-30T22:00:08Z")

</div>

Manu L. wrote:

> def try\_to\_login(antonio)  
> antonio = users(:one)  
> #post :login, :user =\> { :screen\_name =\> user.screen\_name, :email =\>  
> user.email, :password =\> user.password }  
> #post :login, :screen\_name =\> antonio.screen\_name, :email =\>  
> antonio.email, :password =\> antonio.password  
> assert logged\_in?  
> end
> 
> If I uncomment the first post line, I get "undefined local method or  
> variable ‘user’ for UserControllerTest, even though I have a model  
> called User.rb with a table called users in my database.  
> However, if I comment that line and uncomment the second post line,  
> everything works like a charm… could anyone tell me why?

user.screen\_name # where is user defined?

The problem is exactly what the error message is telling you “undefined  
local method or variable ‘user’.”

antonio is defined on the first line of the method so  
antonio.screen\_name is perfectly valid.

---

<div class="post-metadata">

**Author:** ![noloman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/a9a28c/32.png) [@noloman](https://www.ruby-forum.com/u/noloman)\
**Post date:** [July 30, 2010, 10:32pm UTC](https://www.ruby-forum.com/t/testing-question-try-to-login/192476/3 "2010-07-30T22:32:06Z")

</div>

Robert W. wrote:

> Manu L. wrote:
> 
> > def try\_to\_login(antonio)  
> > antonio = users(:one)  
> > #post :login, :user =\> { :screen\_name =\> user.screen\_name, :email =\>  
> > user.email, :password =\> user.password }  
> > #post :login, :screen\_name =\> antonio.screen\_name, :email =\>  
> > antonio.email, :password =\> antonio.password  
> > assert logged\_in?  
> > end
> > 
> > If I uncomment the first post line, I get "undefined local method or  
> > variable ‘user’ for UserControllerTest, even though I have a model  
> > called User.rb with a table called users in my database.  
> > However, if I comment that line and uncomment the second post line,  
> > everything works like a charm… could anyone tell me why?
> 
> user.screen\_name # where is user defined?
> 
> The problem is exactly what the error message is telling you “undefined  
> local method or variable ‘user’.”
> 
> antonio is defined on the first line of the method so  
> antonio.screen\_name is perfectly valid.

Definitely, now I know what fails: I hadn’t defined in the setup method  
at the beginning of the file the valid\_user.

Thanks a lot Robert!
