We have a large application in Ruby on Rails with many filters.  Some of these filters can be complex.  I am looking for a way to individually test these filters with a unit test.  Right now I test them by testing them through an action that uses them with a functional test.  This just doesn't feel like the right way.
Does anyone have advice or experience with this?
views:
1035answers:
4
                
                A: 
                
                
              It depends on what your filters are doing.
This: http://www.movesonrails.com/articles/2008/01/23/spec-ing-your-application-controller
And also learning how to use mocha will get you a long way.
                  jonnii
                   2008-10-30 18:36:35
                
              That is good for testing if the filter is in the list for a given action, but it doesn't test the code of the filter itself.
                  ScottD
                   2008-10-30 18:52:59
                
                +3 
                A: 
                
                
              
            Remember a filter is just a method.
Given this:
class SomeController
  before_filter :ensure_awesomeness
  ...
end
There's no reason you can't just do this:
SomeController.new.ensure_awesomeness
and then check that it calls redirect_to or whatever it's supposed to do
                  Orion Edwards
                   2008-10-30 20:21:09
                
              
                
                A: 
                
                
              
            Orion I have messed with doing that in a few occurrences. Also, most of the time filters are private so you have to do a send:
SomeController.new.send(:some_filter)
                  ScottD
                   2008-10-30 22:25:21
                
              
                
                A: 
                
                
              
              
            I have a post on unit testing before_filters easily, you may wish to take a look. Hope it will help.