如何在Rails 3中的过滤方法之前测试应用程序控制器?

p.m*_*los 6 testing ruby-on-rails before-filter functional-testing ruby-on-rails-3

我有一个before_filter在我的ApplicationController班,我想为它编写一个测试?我应该把这个测试写在哪里?我不想进入每个子类控制器测试文件并重复有关此过滤器的测试.

因此,测试ApplicationControllerbefore_filters 的推荐方法是什么?

请注意,我用Rails 3.2.1minitest.

bma*_*ddy 7

我的情况与你的情况略有不同,但我需要做一些类似于整个网站测试身份验证的事情(使用Devise).我是这样做的:

# application_controller.rb
class ApplicationController < ActionController::Base
  before_filter :authenticate_user!
end

# application_controller_test.rb
require 'test_helper'

class TestableController < ApplicationController
  def show
    render :text => 'rendered content here', :status => 200
  end
end

class ApplicationControllerTest < ActionController::TestCase
  tests TestableController

  context "anonymous user" do
    setup do
      get :show
    end
    should redirect_to '/users/sign_in'
  end
end
Run Code Online (Sandbox Code Playgroud)

如果有特定的控制器需要跳过前过滤器,我将进行测试以确保他们在特定控制器的测试中跳过它.这不是你的情况,因为我对该方法的效果感兴趣,而不仅仅是知道它被调用,但我想我会分享以防你发现它有用.