Question
How do you install / mock the rspec routing test to look more like a production rack environment?
Bonus points for the ability to first call the controller authentication function (for example, ApplicationController.authenticate_user to set the session [: current_user_id]
More details
I use route: restrictions to have a www.example.com/ route to two different controllers and views depending on whether the user is registered or not.
root :to => 'intentions#latest', :constraints => lambda {|r| r.env['rack.session'].has_key?(:current_user_id) }
root :to => 'welcome#index'
I wanted to make sure that this bit of fragile routing has the proper test around it. The keys are testing it, but I wanted to test a little more deeply, especially when I messed up: current_user_id, and the cookies did not catch it. For example, I pushed my finger: current_user_id in my authenticate_user () method of ApplicationController. I would like to call it in the previous one (: each) and make sure that I am setting the correct session key.
So, I created a file called / spec / routing / auth _routing_spec.rb and tried to follow the manual at http://relishapp.com/rspec/rspec-rails/v/2-6-rc/dir/routing-specs . I created the following specification:
it "/ should send me to home page" do
get('/').should route_to(:controller => :index)
end
When I run spec, I run this error on a route with a restriction :. I assume that env ['rack.session'] does not exist. I tried to make fun of the request object through Rails.application.call(Rack::MockRequest.env_for('/')), but that did not help.
Failure/Error: get('/').should route_to(:controller => :index)
NoMethodError:
undefined method `has_key?' for nil:NilClass
Details of a boring applicationrake about gives
Ruby version 1.9.2 (x86_64-darwin10.6.0)
RubyGems version 1.6.2
Rack version 1.2
Rails version 3.0.7
Active Record version 3.0.7
Action Pack version 3.0.7
Active Resource version 3.0.7
Action Mailer version 3.0.7
Active Support version 3.0.7
grep rspec gemfile.info gives
remote: git://github.com/rspec/rspec-rails.git
rspec-rails (2.6.0.rc6)
rspec (= 2.6.0.rc6)
rspec (2.6.0.rc6)
rspec-core (= 2.6.0.rc6)
rspec-expectations (= 2.6.0.rc6)
rspec-mocks (= 2.6.0.rc6)
rspec-core (2.6.0.rc6)
rspec-expectations (2.6.0.rc6)
rspec-mocks (2.6.0.rc6)
rspec-rails!