- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嘿,我目前正在使用 Rails 中内置的 minitest 框架。尝试在我的 ApplicationController 中围绕 protect_from_forgery 测试一些方法并从 InvalidAuthenticityToken 异常中恢复。作为引用,我的 ApplicationController 看起来像:
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
rescue_from ActionController::InvalidAuthenticityToken, with: :handle_invalid_token
def access_denied(exception)
redirect_to root_path, :alert => exception.message
end
protected
def handle_invalid_token
flash[:alert] = I18n.translate('devise.failure.invalid_token')
redirect_to new_user_session_path
end
end
我正在寻找方法来测试 rescue_from ActionController::InvalidAuthenticityToken 和 protect_from_forgery with::exception 方法。是否可以用 minitest 模拟其中的一些东西,请原谅我的经验仅限于基本模型/ Controller / View 测试。
这里不多,但我想我会包括我的 ApplicationControllerTest 的类
require 'test_helper'
class ApplicationControllerTest < ActionController::TestCase
test 'invalid access token' do
end
end
最佳答案
我通过像这样删除一个测试 Controller 来做到这一点:
class StubController < ApplicationController
def authenticate_user
authenticate_user!
head 200
end
def authenticate_user_invalid
authenticate_user!
end
end
Rails.application.routes.disable_clear_and_finalize = true
# Create a new route for our new action
Rails.application.routes.draw do
get 'authenticate_user', to: 'stub#authenticate_user'
get 'authenticate_user_invalid', to: 'stub#authenticate_user_invalid'
end
class StubControllerTest < ActionController::TestCase
test 'authenticate_user sets current_user if valid user token and email' do
user = users(:authenticatable_user)
@request.headers['Authorization'] = "Token token=#{user.token},email=#{user.email_address}"
get :authenticate_user
assert_equal user, assigns(:current_user)
end
end
stub Controller 只是 ApplicationController
的子类,然后我将路由添加到将触发我想要测试的实际方法的虚构操作。如果一切按计划进行,您可以测试副作用以查看其是否有效。希望这能为您提供足够的信息,您可以对其进行破解以满足您的需求。
关于ruby-on-rails - ApplicationController 中用于 rescue_from InvalidAuthenticityToken 的 Rails Minitest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34002285/
我正在使用 Ember App Kit (EAK)。 当我加载我的索引页面时,Ember Inspector/Chrome Dev Tools 控制台显示这一行: *generated -> cont
在应用程序 Controller 中,我有几个方法可以处理请求的 Controller 和操作名称。 为了遵循 DRY 原则,我想用这些参数定义共享变量。 class ApplicationContr
每当两个并发的 HTTP 请求进入我的 Rails 应用时,第二个总是返回以下错误: A copy of ApplicationController has been removed from the
ApplicationController是否继承自DeviseController? 否则如何设计方法在 ApplicationController 中可用。 最佳答案 ApplicationCon
在我的 ApplicationController 中,我有一个方法定义为辅助方法: helper_method :some_method_here 我如何在 RSpec 中测试 Applicatio
如果我在 application_controller 中: def calendar_sidebar @missed_dates_by_date = current_user.misse
我需要对 Controller 中的回调顺序进行更精细的控制。目前 Rails 只允许您使用 append|prepend_before|after_action,但如果您想添加一个带有专用回调的模块
我在ApplicationController中定义了方法 class ApplicationController < ActionController::Base helper_method
我在 rackspace 云服务器上在线设置了一个简单的应用程序,但我从 phusion passenger 收到以下错误消息: uninitialized constant ApplicationC
我正在使用 Rails 5.2、Pundit 1.1 和 rails_admin 1.2 我的 application_controller.rb 中有以下内容: class ApplicationC
我正在尝试使用 rspec 来测试我的 ApplicationController 中的过滤器。 在 spec/controllers/application_controller_spec.rb我有
我是 Rails 的新手,我不明白为什么会出现此错误: undefined local variable or method `with' for ApplicationController:Clas
我的问题涉及以下开发堆栈: rails 3.2.1 德雷珀 0.14 祖先 1.2.5 我想要做的是,将导航传送到我的布局。所以我在 ApplicationController 中定义了一个 befo
我想我会想出一个巧妙的方法来在 Rails 3.x gem 中扩展 ApplicationController。 在我的 gem lib/my_namespace/my_controller.rb ,
我正在使用 Ember.js,并且在渲染的模板部分中从我的 ApplicationController 访问方法时遇到问题。 在我的布局中,我有以下内容 {{ render common/na
在Rails中,我在ApplicationController中添加了几个rescue_from处理程序。他们的工作正常,除了看起来像 session 丢失,因为下一个请求始终被重定向到登录页面。 A
假设我有两个模型:客户和产品 Client的“username”和“email”应该是“unique index”,作为Product的“serialnumber” 当用户在作为唯一索引的表单字段上键
当我从像 UsersController < ApplicationController 这样的继承 Controller 调用位于 ApplicationController 内部的方法时,该方法的
我正在尝试升级到 Rails 4 beta 1,但遇到了一些问题。 简而言之,这就是我的应用程序 Controller 的样子。 class ApplicationController ': unde
我需要在 Rspec/capybara 请求规范中 stub current_user 方法的响应。该方法在 ApplicationController 中定义并使用 helper_method。该方
我是一名优秀的程序员,十分优秀!