> Is SplashController#get_user defined as a method in its own right? And do
> any of the implementations of #get_user take any parameters?
The method is defined only on ApplicationController (the parent of
SplashController and others), and takes no parameters.
> As a sanity check, you could move the ApplicationController#get_user method
> to a class method on a new class e.g. OAuthService.get_user and mock that
> instead.
This method is actually a filter that runs in front of all actions in
my controllers (which is why it's defined way up on the parent
controller). A little bit more detail: The method looked like this:
def get_user
@user ||= unless session[:id].blank?
begin
User.find(session[:id])
rescue ActiveRecord::RecordNotFound
nil
end
end
end
The idea is to not bother hitting the database if there's no
information in the session. The problem was I wanted to stub this
method to return a (mocked) user regardless of session data. In the
end, I changed the method to this:
def get_user
begin
@user ||= User.find(session[:id])
rescue ActiveRecord::RecordNotFound
nil
end
end
Then I stub User.find and I'm on my way. Thankfully ActiveRecord
(which User extends) doesn't bother going to the DB when you pass nil
to find() so I'm still saved the DB hit if the user isn't logged in,
with more readable code. And I can test it.
> If you are still having trouble, could you try and reproduce the unexpected
> behaviour in a simple (non-Rails) test, post it here, and we can try and
> help.
I'm not, because I negated my need to do it. But for others who might
be interested, the code at the following URL reproduces the issue in a
dirt-simple non-Rails case:
--
Bill Kocik