On 6-jun-2006, at 8:28, Robert MannI wrote:
Any ideas how I can circumvent that?
Or are functional tests really not suited for any kind of logins
and session work? I don’t see why I should move everything to a
Integration Test just because I added user logins. ]
Looks like you really want a publci method instead of a protected one.
def get_stuff
# do foo
end
hide_action :get_stuff
This will make it a public method and not a callable action. Another
option would be to just use send to override the access.
@controller.send :logged_in_user # bla bla bla
However, if you want t manage authentication in your tests, it’s a
much better idea to actually make your test go through authentication
all by itself, I do it with this method in my test case:
def with_user(slug, &block)
passwords = { :john => ‘secret’ }
begin
if @request
@previous_host = @request.host
@request.host = “#{slug}…mydomain.net”
with_http_auth(slug, passwords[slug.to_sym]) do
yield
end
else
yield
end
ensure # Sometimes the after_filter does not fire - if a
controller throws up, for instance, or the test throws
# do any cleanup here
@request.host = @previous_host if @request
end
end
Then, to test something with a specific user you do
get :index
assert_response 401
with_user :john do
get :index
assert_success
end
You can also make your method supply the actual User object to the
block to compare against it
with_user :john do | john |
get :index
assert_success
assert_equal assigns(:user), john
end
–
Julian ‘Julik’ Tarkhanov
please send all personal mail to
me at julik.nl