加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 23:16:17 所属栏目:百科 来源:网络整理
导读:我的ApplicationController类上有一个before_filter,我想为它编写一个测试?我应该把这个测试写在哪里?我不想进入每个子类控制器测试文件并重复有关此过滤器的测试. 因此,测试ApplicationController before_filters的推荐方法是什么? 请注意,我使用的是带
我的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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读