プライベート ApplicationController メソッドを利用するアクションをテストする方法を理解するのに苦労しています。説明する:
ユーザーは (役割を通じて) 多くの組織を持ち、所属します。逆もまた同様です。組織には関連するエンティティが多数ありますが、アプリではユーザーは一度に 1 つの組織しか扱っていないため、すべてのコントローラー メソッドで現在の組織をスコープにしたいと考えています。だから私のApplicationControllerで:
private
# Returns the organisation that is being operated on in this session.
def current_org
# Get the org id from the session. If it doesn't exist, or that org is no longer operable by the current user,
# find an appropriate one and return that.
if (!session[:current_organisation_id] || !current_user.organisations.where(['organisations.id = ?', session[:current_organisation_id]]).first)
# If an org doesn't exist for this user, all hope is lost, just go to the home page
redirect_to '/' if (!current_user.organisations.first)
# Otherwise set the session with the new org
session[:current_organisation_id] = current_user.organisations.first.id;
end
# Return the current org!
current_user.organisations.first
end
まず最初に、これがスコープを設定する最良の方法であるかどうかはわかりません。エレガントな default_scope が欲しいのですが、セッション変数を使用すると問題があるようです。とにかく、上記はコントローラーでこれをやらせてくれます:
class HousesController < ApplicationController
def index
@houses = current_org.houses
end
end
そこで、rspec を使用してコントローラーをテストしたいと思います。current_org メソッドを呼び出して factory_girl 組織モデルの 1 つを返すことを確認して、仕様を記述できるようにするにはどうすればよいですか。ファクトリ、スペック、アクション、および AppControlled メソッドをどのように組み合わせるのが最善かについて、私は混乱しています。