# Functional testing with act as authenticated

**URL:** <https://www.ruby-forum.com/t/functional-testing-with-act-as-authenticated/115293>\
**Category:** Rails\
**Created:** [September 23, 2007, 9:38am UTC](https://www.ruby-forum.com/t/functional-testing-with-act-as-authenticated/115293 "2007-09-23T09:38:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [September 23, 2007, 9:38am UTC](https://www.ruby-forum.com/t/functional-testing-with-act-as-authenticated/115293/1 "2007-09-23T09:38:55Z")

</div>

I’m trying to run some functional tests for actions that are behind  
authentication (via act as authenticated plugin), but am having  
problems getting “current\_user” set for the controller.

Here’s the test code:

def test\_create  
num\_notes = Note.count

```
  post :create, :note => {:title => "Test Note", :msg => "Test

```

Message" }  
assert\_response :redirect  
assert\_redirected\_to :action =\> ‘list’

```
  assert_equal num_notes + 1, Note.count

```

end

Here’s the contoller code that it is failing:

@note.user\_id = current\_user.id

I’ve added this include to the first line of the test class:

include AuthenticatedTestHelper

In the setup function I’ve added:

login\_as :jeremy

There is a user in the fixure with the login of “jeremy”. So what am  
I doing wrong?

---

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [September 23, 2007, 9:51am UTC](https://www.ruby-forum.com/t/functional-testing-with-act-as-authenticated/115293/2 "2007-09-23T09:51:55Z")

</div>

I found that the default “authenticated\_test\_helper.rb” file was  
throwing an error about the “users” method being undefined with the  
following function:

def login\_as(user)  
@request.session[:user] = user ? users(user).id : nil  
end

So I changed it to this:

def login\_as(user)  
@request.session[:user] = user ? User.find\_by\_login(user) : nil  
end

This seemed to fix the problem. Is this an OK modification to make?  
Why didn’t the code that it shipped with work?

---

<div class="post-metadata">

**Author:** ![Mozmonkey](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@Mozmonkey](https://www.ruby-forum.com/u/Mozmonkey)\
**Post date:** [September 23, 2007, 4:56pm UTC](https://www.ruby-forum.com/t/functional-testing-with-act-as-authenticated/115293/3 "2007-09-23T16:56:37Z")

</div>

Mozmonkey wrote:

> I found that the default “authenticated\_test\_helper.rb” file was  
> throwing an error about the “users” method being undefined with the  
> following function:

And you are using the out-of-the-box fixtures system, with fixtures  
:users  
above every call to login\_as?

> This seemed to fix the problem. Is this an OK modification to make?  
> sessions should only contain numbers (for various definitions of  
> “should”),  
> so you are missing an .id

> Why didn’t the code that it shipped with work?

Works for me. How sure are you that this assertion would pass before  
your  
login\_as?

```
assert_not_nil users(:jeremy)

```

Then, I suspect something reconstitutes current\_user at page hit time,  
via  
User.find\_by\_id(session[:user])

Storing entire objects in your session table is bad JuJu.

–  
Phlip  
[http://www.oreilly.com/catalog/9780596510657/](http://www.oreilly.com/catalog/9780596510657/)  
“Test Driven Ajax (on Rails)”  
assert\_xpath, assert\_javascript, & assert\_ajax
