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

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 如何在Rails 3中的过滤方法之前测试应用程序控制器?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的ApplicationController类上有一个before_filter,我想为它编写一个测试?我应该把这个测试写在哪里?我不想进入每个子类控制器测试文件并重复有关此过滤器的测试.

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

请注意,我使用的是带有minitest的Rails 3.2.1.

解决方法

我的情况与你的情况略有不同,但我需要做一些类似于整个网站测试身份验证的事情(使用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

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

原文链接:https://www.f2er.com/ruby/264913.html

猜你在找的Ruby相关文章