【发布时间】:2010-11-11 23:52:35
【问题描述】:
您好,我最近接手了一个项目,之前的开发者对 Rails 不熟悉,因此决定将很多重要的逻辑放入视图助手中。
class ApplicationController < ActionController::Base
protect_from_forgery
include SessionsHelper
include BannersHelper
include UsersHelper
include EventsHelper
end
特别是会话管理。这没关系,可以使用该应用程序,但我在为此编写测试时遇到问题。
一个具体的例子。一些操作执行before_filter 以查看current_user 是否是管理员。这个current_user 通常由我们所有控制器共享的sessions_helper 方法设置
所以为了正确测试我们的控制器,我需要能够使用这个current_user 方法
我试过这个:
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 相同的方式访问
我怎样才能欺骗这个疯狂的设置来测试?
【问题讨论】:
-
在
AppsControllerTest类中做include SessionsHelper没有帮助?如果你打算在这个应用程序上积极开发,我强烈建议重构他们编写的代码,这没有意义,你会节省时间。听起来你只是想保持它运行,但没有时间投资。 -
所以看起来帮助器被包括在内,因为应用程序控制器调用了 user_helper 中的一个方法,然后在 session_helper 中触发了一个错误。所以看起来他们好像被包括在内但不能互相交谈。z
标签: ruby-on-rails unit-testing ruby-on-rails-3 functional-testing