setting acting_user in tests

4 views
Skip to first unread message

Steve DeBaun

unread,
Jan 9, 2009, 3:20:03 PM1/9/09
to Hobo Users
I want to be able to set acting_user to a specific user during a
test. Can this be done easily?

Cheers,

sd

Tiago Franco

unread,
Jan 9, 2009, 4:34:19 PM1/9/09
to Hobo Users
Ups! I sent the response in a private e-mail instead of making a post
here.
---

are you testing models or controller?

If you are testing the model, you'll have to pass the user in a
method. The method shall then use:

with_acting_user(user) {

yada yada yada

}


For example:

ChampionshipControllerTest:

def test_viewable_by_admin
assert_view(:championship_with_games, :admin, true)
end

private
def assert_view(championship_label, viewing_user_label,
expected_result)
championship = championships(championship_label)
user = users(viewing_user_label)
assert_equal expected_result, championship.viewable_by?(user)
end


If you take a closer look at the viewable_by? implemented by Hobo. You
see:


(from permissions.rb)
def viewable_by?(user, attribute=nil)
...
with_acting_user(user) { view_permitted?(attribute) }
end


To test the controller, you'll have to login the user first using:

post :login, :login => 'championship_manager', :password => 'test'

TF

roland oth

unread,
Jan 11, 2009, 6:44:03 AM1/11/09
to hobo...@googlegroups.com
Hi
Has anybody got a sample of a controller test file (ideally with
shoulda) that shows how the
login action is performed so that the subsequient get/post action is
done in a logged in state.
I am particularly interested how to set up the user in the tests. (not
via Fixtures)

My current setup is here, http://gist.github.com/45694 but the tests
do not work when the veiwable
restrictions are set to more complex things than a simple true
(everybody can view)

I would like to set up the tests as there are different roles in the
app and I think doing
controller tests would be the best place to put in the desired results.

Thanx
Reply all
Reply to author
Forward
0 new messages