I recently needed to test a thankyou page for a form. The previous action sets a flash value, flash[:email], and then redirects to this page. Testing the page by itself requires that this flash be set. Here’s how to do it:
get :thankyou, {}, nil, {:email => 'test@test.com'}
This will ensure that when the page is called, it has the correct flash elements set. This is important because we’re calling the page directly, not redirecting from a previous action that would have set the flash automatically.
The test methods to call actions (get, post, put, delete) actually take four parameters, even though we typically only use two. The first is the action, and the second is the parameter list. We normally don’t put curly braces around the parameter list because Ruby is smart enough to know that if it sees a bunch of key-value pairs at the end of a method call, it should scoop them up into a single hash. We can’t do that for our example because parameter, session, and flash data need to be passed in different hashes.
For more details visit the same page I did, A Guide to Testing Rails Applications.
Tags: delete, developers, flash, get, parameters, post, put, Testing
March 4, 2010 at 4:50 pm |
Thanks for this!
BTW– your code example has a > instead of a > — too may levels of escaping :)