Functional testing with act as authenticated

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?

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?

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?

  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?

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.