Test to check that before filter is being run?

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

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?

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

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