# Test to check that before filter is being run?

**URL:** <https://www.ruby-forum.com/t/test-to-check-that-before-filter-is-being-run/108420>\
**Category:** Rails\
**Created:** [July 3, 2007, 11:03am UTC](https://www.ruby-forum.com/t/test-to-check-that-before-filter-is-being-run/108420 "2007-07-03T11:03:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [July 3, 2007, 11:03am UTC](https://www.ruby-forum.com/t/test-to-check-that-before-filter-is-being-run/108420/1 "2007-07-03T11:03:17Z")

</div>

Hi,

On certain controllers I have various before filters, is there anything  
I can put in my functional tests to make sure these before filters are  
run?

Thanks,  
Mark

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [July 3, 2007, 2:16pm UTC](https://www.ruby-forum.com/t/test-to-check-that-before-filter-is-being-run/108420/2 "2007-07-03T14:16:12Z")

</div>

What do these before filters do? Here’s an example:

Say I have a before\_filter called “find\_my\_record” since my edit, show,  
delete, and update all call @user = User.find(params[:id])

before\_filter :find\_my\_record, :only=\>[:edit, :update, :show, :delete]

…

def edit  
end

def delete  
@user.destroy  
end

protected  
def find\_my\_record  
@user = User.find(params[:id])  
end

Then I have a functional test that looks like this:

def test\_edit  
get :edit, {:id=\>1}  
assert\_response :success  
assert\_not\_nil assigns(:user) # this should be filled in if the  
before  
filter ran.  
end

Basically, it all comes down to what your before\_filter is actually  
doing.  
If it redirects certain users away, write a test helper that does that.

def test\_should\_not\_let\_normal\_users\_delete  
get :delete, {:id=\>1} # note no session passed in  
assert\_response :redirected  
assert\_equal flash[:notice], “You do not have access to that  
feature.”

end

def test\_should\_let\_admins\_delete  
get :delete, {:id=\>1}, {:user =\> 1} # should get this from a fixture  
instead of hard-coding  
assert\_response :redirected  
assert\_equal flash[:notice], “Record deleted successfully.”  
end

Does that help?

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [May 26, 2009, 5:50am UTC](https://www.ruby-forum.com/t/test-to-check-that-before-filter-is-being-run/108420/3 "2009-05-26T05:50:08Z")

</div>

I wrote a simple code to test all your before filters here at

> **[Unit/Functional Test Rails ActionController filters following DRY](http://smsohan.blogspot.com/2009/05/unit-test-actioncontroller-filters.html)**
>
> At ScrumPad most of our controllers are bounded by filters for authentication/authorization. Some filters apply to all actions in a control...

You may find it useful. For each filter in place, you will just write  
a single line of code to test that.
