アプリケーション コントローラー内のテスト用に私が見つけた最も近いリソースはhereです。残念ながら、実際には機能しません。MyApp は初期化されていない定数を生成します。適切なルートを追加する他の方法を試しましたが、どれも機能しません。
私の目的は、ApplicationController に埋め込んだ認証方法をテストすることです。テストを他のコントローラーに入れたくありません。それは彼らの関心事ではないからです。それは彼らをサポートするものですが、「彼らの」機能の一部ではありません。「test_helper」が必要
class TestController < ApplicationController
def index; end
end
class AppControllerTest < ActionController::TestCase
Rails.application.routes.draw do
controller :test do
get 'test/index' => :index
end
end
setup do
@controller = TestController.new
end
should "return nil if there is no district subdomain" do
get :index
assert_equal 'bar', @controller.send(:foo)
end
end
上記のコードでは、url ヘルパー メソッド エラーが発生します。
ActionController::UrlGenerationError: No route matches {:action=>"index", :controller=>"test"}
編集:
ボルダーの親切なアドバイスのおかげで、私はこれを乗り越えましたが、後でルートを修正するという彼の提案はうまくいきません:
teardown do
Rails.application.reload_routes!
end