您好我最近繼承了一個項目,其中前開發人員不熟悉rails,並決定將許多重要的邏輯放入視圖助手中。如何將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
要求聲明發現session_helper.rb
沒問題,但沒有Rails的魔法,它不能以相同的方式訪問AppsControllerTest
我該如何欺騙這個瘋狂的設置來測試?
在AppsControllerTest類中使用'SessionsHelper'並沒有幫助?如果您要積極開發此應用程序,我強烈建議您重新編寫由他們編寫的代碼,這沒有意義,您將節省時間。這聽起來像你只是想讓它繼續運行,並沒有時間去投資。 – Jeremy 2010-11-12 01:07:37
所以看起來像助手正在被包含,因爲應用程序控制器在user_helper中調用一個方法,然後在sessions_helper中觸發一個錯誤。所以它好像被包括在內但不能與對方交談.z – kevzettler 2010-11-12 21:38:58