Ok, so now I''m learning about testing. After a few initial hiccups,
I''ve got my tests written nicely and all the assertions are passing.
But I just have one question about doing tests in a loop.
In my setup, I''m using the login generator to protect
''sensitive''
actions, while some actions are public. So I''ve got this line in my
setup method:
@request.session["user"] = User.find_by_login "bob"
So now all the tests can successfully test actions that require a log
in. But, for security''s sake, I''d like to test that actions
while
logged out, do in fact redirect to the login screen.
I originally wrote the test method like this:
def test_loggedout
@request.session["user"] = nil
process :new
assert_redirected_to :controller => ''account'', :action
=> ''login''
process :edit
assert_redirected_to :controller => ''account'', :action
=> ''login''
...
end
And so on for all my protected actions. Hopefully it''s obvious that
this violates DRY, having so many identical "assert_redirected_to"
calls repeated. I want to put them in a loop, but i''m not entirely
sure how to go about doing it...
This is my first attempt at putting it in a loop:
def test_loggedout
@request.session["user"] = nil
test_show
[
process(:index),
process(:list),
process(:new),
process(:create, ''page'' => {"article" =>
"foo", "title" => "bar" }),
process(:edit, ''id'' => 1),
process(:update, ''page'' => { ''id''
=> 1 }),
process(:destroy, ''id'' => 1)
].each {|act| assert_redirected_to :controller =>
''account'',
:action => ''login'' }
end
The idea here is that we want to re-run the "test_show" method after
logging out, to make sure that it still works fine for logged-out
people. But all the other methods are protected, so I iterate over
each one, and then do the assert. When I run this, it works fine and
all the assertions pass.
But, when I change the controller to not require login for the list
action, all of the assertions are still true. So what''s really
happening here is that I''m simply asserting the redirection 7 times
for the destroy action, not asserting the redirection once for each
action. What is the proper way to iterate over the "process" methods
and apply the same assertion to each?
Thanks.
--
One Guy With A Camera
http://rbpark.ath.cx
On Sun, 6 Mar 2005 00:44:03 -0700, Rob Park <rbpark-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> But, when I change the controller to not require login for the list > action, all of the assertions are still true. So what''s really > happening here is that I''m simply asserting the redirection 7 times > for the destroy action, not asserting the redirection once for each > action. What is the proper way to iterate over the "process" methods > and apply the same assertion to each?Heh, I should stop asking questions so early... I have this bad habit of running into a problem, asking questions first, then finding the answer on my own. If nothing else, perhaps this could serve as a reference for somebody in the same situation as me. Here is the code that works: def test_loggedout @request.session["user"] = nil test_show [ proc { process(:index) }, proc { process(:list) }, proc { process(:new) }, proc { process(:create, ''page'' => {"article" => "foo", "title" => "bar" }) }, proc { process(:edit, ''id'' => 1) }, proc { process(:update, ''page'' => { ''id'' => 1 }) }, proc { process(:destroy, ''id'' => 1) } ].each {|act| act.call; assert_redirected_to :controller => ''account'', :action => ''login'' } end And since my controller is still not requiring login for the "list" action, the assertion fails, which indicates to me that this loop is working as expected*. Though I wonder if there''s a better way to do it without saying "proc" so many times...? * kinda funny, deliberately breaking the controller to make sure the tests are working... what we need are tests for the tests ;) -- One Guy With A Camera http://rbpark.ath.cx
On 06-Mar-2005, at 03:02, Rob Park wrote:> Here is the code that works: > > def test_loggedout > @request.session["user"] = nil > > test_show > > [ > proc { process(:index) }, > proc { process(:list) }, > proc { process(:new) }, > proc { process(:create, ''page'' => {"article" => "foo", "title" > => "bar" }) }, > proc { process(:edit, ''id'' => 1) }, > proc { process(:update, ''page'' => { ''id'' => 1 }) }, > proc { process(:destroy, ''id'' => 1) } > ].each {|act| act.call; assert_redirected_to :controller => > ''account'', :action => ''login'' } > endI appreciate your deserve to comply with DRY, but other concerns (perhaps more important) are maintenance and understandability. I could do the tests individually, or write a little routine to make it somewhat easier: # untested def assert_process_redirected_to(action, destination_controller, destination_action) process action assert_redirected_to :controller => destination_controller, :action => destination_action end def test_loggedout @request.session[''user''] = nil assert_process_redirected_to :new, ''account'', ''login'' assert_process_redirected_to :edit, ''account'', ''login'' def There is also documentation on writing your own assertions somewhere, haven''t looked at it myself, but that might be a good place to start. If you don''t like the above, you could try: # untested for test in [:new, :edit, :list] do process test assert_redirected_to :controller => ''account'', :action => ''list'' end or even: # untested def assert_processes_redirect_to(actions, dest_cont, dest_action) for test in actions do process test assert_redirected_to :controller => dest_cont, :action => dest_action end end def test_logged_out assert_processes_redirect_to [:new, :list, :edit], ''account'', ''list'' end Regards, JJ
Rob Park <rbpark-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I originally wrote the test method like this: > > def test_loggedout > @request.session["user"] = nil > > process :new[...] Where is the ''process'' method documented? I can sort of guess what it does, but my searches for any documentation have all come up empty. -- Regards, Stian Grytøyr
Possibly Parallel Threads
- What is this failure trying to tell me?
- [PATCH server] fixed functional tests after recent controller refactoring and managed node controller fixes.
- Problem with assert_redirected_to
- "the number of parameters does not match the number of substitutions" error
- Object.stubs doesn''t seem to work.