On May 9, 2011, at 6:40 PM, Fearless Fool wrote:
> My app has some logic that permits some actions before the user logs in,
> and I depend upon session[:session_id] as a handle for some state.
>
> My requests testing fails because session[:session_id] is null. I
> attempted a somewhat brute force approach of putting this in my
> specs/requests/authentication_spec.rb file:
>
>
> require 'spec_helper.rb'
>
> class ApplicationController < ActionController::Base
> def self.stub_session_id
> session # force lazy load
> session[:session_id] ||= "kaboodle"
> end
> end
> ...
>
> but when I call stub_session_id, I get a warning and an error:
>
> DEPRECATION WARNING: Disabling sessions for a single controller has been
> deprecated. Sessions are now lazy loaded. So if you don't access them,
> consider them off. You can still modify the session cookie options with
> request.session_options. (called from stub_session_id at
> spec/requests/authentication_spec.rb:5)
>
> and:
>
> Failure/Error: session[:session_id] ||= "kanoodle"
> TypeError:
> can't convert Symbol into Integer
>
> I'm sure this is obvious to those skilled in the dark arts of rspec and
> rails, but I've not passed my initiation rites yet. Any clues on how I
> can stub session[:session_id]?
The session gets initialized the first time you assign something to it, but I'd
recommend using something other than :session_id to hold on to the state you're
interested in. Something like:
before_filter :init_session
def init_session
session[:anonymous_user_data] = "whateveryouneed"
end
The reason is that Rails uses :session_id internally, and you probably want to
stay out of its way. If you print out the session post-request, btw, you'll see
both the :anonymous_user_data key _and_ the :session_id key:
it "does something" do
get whatever_path
p session
end
HTH,
David
_______________________________________________
rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users