如何将Rails助手导入功能测试 [英] How to import Rails helpers in to the functional tests
问题描述
您好,我最近继承了一个项目,该项目的前开发人员对Rails不熟悉,因此决定在视图助手中加入很多重要的逻辑.
Hi I recently inherited a project in which the former dev was not familiar with rails, and decided to put a lot of important logic into the view helpers.
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
include BannersHelper
include UsersHelper
include EventsHelper
end
特别是会话管理.没关系,可以使用该应用程序,但是为此编写测试时遇到了问题.
Specifically session management. This is okay and working with the app but I am having problems writing tests for this.
一个具体的例子.有些操作会执行before_filter
来查看current_user
是否是管理员.此current_user
通常由所有控制器中共享的sessions_helper
方法设置
因此,为了正确测试我们的控制器,我需要能够使用此current_user
方法
A specific example. Some of the actions do a before_filter
to see if a current_user
is an admin. This current_user
is normally set by a sessions_helper
method that is shared in all our controllers
So in order to properly test our controllers I need to be able to use this current_user
method
我已经尝试过了:
require 'test_helper'
require File.expand_path('../../../app/helpers/sessions_helper.rb', __FILE__)
class AppsControllerTest < ActionController::TestCase
setup do
@app = apps(:one)
current_user = users(:one)
end
test "should create app" do
assert_difference('App.count') do
post :create, :app => @app.attributes
end
end
require语句发现session_helper.rb
还可以,但是没有Rails的魔力,就无法以与AppsControllerTest
The require statement finds the session_helper.rb
okay but without the Rails magic it is not accessible in the same way with in the AppsControllerTest
我该如何欺骗这种疯狂的设置进行测试?
How can I spoof this crazy setup to test?
推荐答案
我发现的唯一解决方案是重构并使用像样的auth插件
The only solution i found was to re-factor and use a decent auth plugin
这篇关于如何将Rails助手导入功能测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!