In your tests, you'll need to log in a user otherwise the before_filter
in your controller redirects to the login page.

I suggest you change a portion of your authentication code, in particular

  def logged_in?
    # current_user
    session[:user_id]
  end

So instead of calling current_user, which hits the database to fetch the
User by the way, you can just return session[:user_id].  This is good
enough to figure out if a user is logged in as it will return nil if the key
:user_id is not set, and nil is as good as false in ruby.

With that change, you can then set a session variable in your functional
test, eg

class UnitsControllerTest < ActionController::TestCase
  def test_index
    get :index, { }, { 'user_id' => 1 }
    assert_template 'index'
  end


The first hash after the action is for parameters, and the second one is for
session variables.

Ideally though, you should be testing different scenarios to see what the
behavior will be when a user is logged in or not.

I highly recommend that you look at shoulda and flexmock for testing.
Shoulda allows you to easily nest contexts for your tests while flexmock
allows you to set up mocks to avoid hitting the database or setting up
session variables as done above.

Instead, you can have something like this in your test_helper.rb

def be_logged_in
  flexmock(@controller).should_receive(:logged_in?).and_return(true)
end

def be_logged_out
  flexmock(@controller).should_receive(:logged_in?).and_return(false)
end

Then in your shoulda setup blocks, you just call one of those two helpers
depending on the context you are testing.

Have fun,
Franz

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to