How to check if before_filter is redirected for all actions of a Rails controller?

I have a fairly typical require_no_userlike before_filterin one of my controllers. I need to verify that the registered user is redirected by this filter if they try to access any of the controller actions.

Is there any reasonable way to do this without listing all the controller actions in my test case?

I try to avoid:

context 'An authenticated user' do
  setup do
    activate_authlogic
    @user = Factory(:user)
    UserSession.create(@user)
  do

  should 'not be allowed to GET :new' do
    get :new
    assert_redirected_to(root_path)
  end

  should 'not be allowed to POST :create' do
    # same as above
  end

  # Repeat for every controller action
end
+3
source share
1 answer

Not that I knew ... although you could make it a little shorter by wrapping all the methods and actions in a hash:

should "be redirected" do
  {
    :get => :new,
    :post => :create,
  }.each do |method, action|
    send(method, action)
    assert_redirected_to(root_path)
  end
end

Edit: so yes, this is probably too much, but here it’s different:

should "be redirected" do
  ActionController::Routing::Routes.named_routes.routes.each do |name, route|
    if route.requirements[:controller] == @controller.controller_name
      send(route.conditions[:method], route.requirements[:action])
      assert_redirected_to(root_path)
    end
  end
end

, : , "" , ,

map.resources :foo, :collection => {
  :bar => [:get, :post]
}

GET-.

, URL- , , . , :)

+2

Source: https://habr.com/ru/post/1794006/


All Articles