[rspec-users] [Rails] Controller testing problem

Zach Moazeni zach.lists at gmail.com
Tue Jul 7 08:29:23 EDT 2009


Hey Ignacy,

I think I'm in the minority on this one, but I tend to keep my action  
logic independent of filters. Doing that, my typical controller spec  
begins like this:

require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
require File.expand_path(File.dirname(__FILE__) + '/../spec_helpers/ 
controller_behaviors')

describe SomeController do
   stub_all_filters!

   describe ...

   ...
end

The method "stub_all_filters!" is a custom rolled, per project  
maintained, rspec hack that rips through Rails's internal methods to  
find the filters on a controller and creates a "before do ... end"  
that stubs each filter to "true" [1]

I still use before filters because it's a good fit for many plugins  
such as ssl_requirement and authentication. However 1) I don't  
explicitly split up controller action behavior into filters and 2) the  
controller actions can depend on the filters indirectly by consuming  
methods like "self.current_user"

I know you're maintaining prior-written code, but I thought this would  
be relevant. I hope this helps more than it confuses.

[1] - http://gist.github.com/142047

On Jul 1, 2009, at 4:18 AM, Ignacy Moryc wrote:

> Hi,
>
> I'm working on a legacy app - it was created in 1.2.5 then over time
> ported to 2.3. It has absolutely no tests - and I'm trying to fix that
> in between adding new functionality.
>
> My problem right now is that my ApplicationController is running
> before_filters for setting different request variables (like current
> location, etc.) and this messes up my tests - I can't test responses
> properly (they are all redirects because ApplicationController derives
> from SiteController which actions implement setting variables). What's
> worse I can't test assigns hash - it doesn't get proper values  
> assigned.
>
> Is there a way for RSpec to skip the filters in tests? Or to stop
> ApplicationController to derive from SiteController?
>
> Thanks,
> Ignacy

--
Zach Moazeni
http://simplechatter.com





More information about the rspec-users mailing list