Testing Controllers that use Devise before_filters

I was racking my head off the wall yesterday trying to get my tests to work with Devise.

I am using the Devise supplied authenticate_user! filter on a lot of my controllers to ensure that the user is logged in before they view a given page.

This works exactly as expected when I test it using a browser. If the user attempts to view a page that is protected by the filter then they are redirected┬áto the login page. However if I try to do this within an RSpec test then the result is a “uncought throw :warden” exception.

As far as I can tell there is no good solution to this. So my approach is to use the following RSpec code to verify that the exception is being thrown as this tells me that the Devise code is being executed and the filter is working correctly.

Not perfect but it does the trick for me.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">