如何测试before_filter是否会重定向所有Rails控制器操作?

时间:2022-04-29 17:13:33

I have a fairly typical require_no_user as a before_filter in one of my controllers. I need to test that a logged in user is redirected by this filter if they try to access any of the controller's actions.

我有一个相当典型的require_no_user作为我的一个控制器中的before_filter。如果他们试图访问任何控制器的操作,我需要测试登录用户是否被此过滤器重定向。

Is there a sensible way to do this without enumerating all of the controller's actions in my test case?

如果不在我的测试用例中列举所有控制器的操作,是否有一种合理的方法可以做到这一点?

I'm trying 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

1 个解决方案

#1


2  

Not that I'm aware of... though you could make it a bit shorter by packing all the methods and actions into 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 yeah, this is probably overkill, but here's another way:

编辑:所以是的,这可能是矫枉过正,但这是另一种方式:

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

Seems though that if you define multiple :methods in custom routes that it still only "finds" the first, e.g.

看起来如果你在自定义路线中定义多个:方法,它仍然只“找到”第一个,例如

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

The above route will only be attempted with the GET verb.

只有使用GET动词才能尝试上述路线。

Also if there are other requirements in the URL, such as presence of a record ID, my naive example ignores that requirement. I leave that up to you to hack out :)

此外,如果URL中存在其他要求,例如存在记录ID,我的天真示例将忽略该要求。我把它留给你来解决:)

#1


2  

Not that I'm aware of... though you could make it a bit shorter by packing all the methods and actions into 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 yeah, this is probably overkill, but here's another way:

编辑:所以是的,这可能是矫枉过正,但这是另一种方式:

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

Seems though that if you define multiple :methods in custom routes that it still only "finds" the first, e.g.

看起来如果你在自定义路线中定义多个:方法,它仍然只“找到”第一个,例如

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

The above route will only be attempted with the GET verb.

只有使用GET动词才能尝试上述路线。

Also if there are other requirements in the URL, such as presence of a record ID, my naive example ignores that requirement. I leave that up to you to hack out :)

此外,如果URL中存在其他要求,例如存在记录ID,我的天真示例将忽略该要求。我把它留给你来解决:)