views:

1035

answers:

4

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?

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
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
+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
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
A: 

I have a post on unit testing before_filters easily, you may wish to take a look. Hope it will help.