Testing a Rails ApplicationController method with rspec -
just having trouble figuring out how test action utilizes private applicationcontroller method. explain:
a user has , belongs many organisations (through roles), , vice versa. organisation has bunch of related entities, in app user dealing single organisation @ time, of controller methods want scope current organisation. in applicationcontroller:
private # returns organisation being operated on in session. def current_org # org id session. if doesn't exist, or org no longer operable current user, # find appropriate 1 , return that. if (!session[:current_organisation_id] || !current_user.organisations.where(['organisations.id = ?', session[:current_organisation_id]]).first) # if org doesn't exist user, hope lost, go home page redirect_to '/' if (!current_user.organisations.first) # otherwise set session new org session[:current_organisation_id] = current_user.organisations.first.id; end # return current org! current_user.organisations.first end
first things first, don't know if best way scope. i'd love elegant default_scope thing, use of session variables seems problematic. anyway, above let's me in controllers:
class housescontroller < applicationcontroller def index @houses = current_org.houses end end
so want use rspec test controllers. how can make sure current_org method can called , returns 1 of factory_girl organisation models can write spec. i'm confused @ how best bring factory, spec, action , appcontrolled method together.
Comments
Post a Comment