Setting the flash in tests

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 => ''}

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: , , , , , , ,

One Response to “Setting the flash in tests”

  1. John Says:

    Thanks for this!

    BTW– your code example has a > instead of a > — too may levels of escaping :)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: