test to check that before filter is being run?

51 views
Skip to first unread message

Mark

unread,
Jul 3, 2007, 5:03:17 AM7/3/07
to rubyonra...@googlegroups.com
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

--
Posted via http://www.ruby-forum.com/.

Brian Hogan

unread,
Jul 3, 2007, 8:15:22 AM7/3/07
to rubyonra...@googlegroups.com
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?
Reply all
Reply to author
Forward
0 new messages