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.

Ruby on Rails Tutorial: Learn Rails by Example

I have been kicking around the idea of building a site to store golf scores and report on stats for a few years now and have even gone through the motions of starting to build it in php a couple times. I don’t know if I didn’t have experience at the time or if the documentation/community for the frameworks I choose were not clear enough but the end result was always the same. I would work at it for a few weeks then after not really making any real traction I would quit and forget about it.

This time has been different though, Ruby on Rails just makes sense to me and I haven’t felt overwhelmed and continue to chip away at building the site. I think the biggest reason that I have not given up this time is that I bought “Ruby on Rails Tutorial: Learn Rails by Example” and built out the sample application and did all the extra exercises. This book is recommended over and over again whenever someone asks about learning rails and it really does deserve its reputation as the best resource for learning rails.

It does a really great job of teaching the fundamentals of Model-View-Controller, REST and Test Driven Development, all while getting the reader comfortable with Ruby syntax and Rails conventions.

So my first lesson learned is that anyone who wants to learn rails should start with “Ruby on Rails Tutorial: Learn Rails by Example”.