How to Skip Filters in Controller RSpec Tests

I ran into an interesting dilemma today with a controller spec.

I needed to verify that a post to my controller’s update method did indeed update a particular setting on an instance of one of my models. Unfortunately, the controller I was using inherited some before_filter(s) from its parent that called a method that checked for a logged in user.

But all I wanted was to make sure my model’s setting actually got changed!

Thankfully, skip_before_filter came to the rescue.

As you can imagine, you can use this method in an RSpec test to “skip” a particular filter from a specific controller. For instance, let’s say you are setting up an instance of your controller in your tests like so:

let(:subject) { }

And lets say DogController has a before_filter that looks something like this:

before_filter :authorize, :only => :delete

What this means is that before the delete method is ever called, authorize will be called first. But you want to test something in the delete method without having authorize called.

All you need to do is something like this:

it "should test something in the delete method" do

  subject.class.skip_before_filter :authorize

  #some test stuff


Hope this helps if you find yourself in a situation where you need to skip some filters 🙂

Tagged , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: