Testing Rail's route's :constraints lambda with RSpec 2 -


question:

how setup/mock rspec routing test more production rack environment?

bonus points being able call controller's authentication function first (eg applicationcontroller.authenticate_user set session[:current_user_id]

details:

i using route :constraints have www.example.com/ route 2 different controllers & views depending on if user logged in or not.

root :to => 'intentions#latest', :constraints => lambda {|r| r.env['rack.session'].has_key?(:current_user_id) } root :to => 'welcome#index' 

i wanted make sure bit of fragile routing has proper test around it. cukes testing wanted bit deeper of testing, messed :current_user_id , cukes did not catch that. example fat fingered :current_user_id in applicationcontroller's authenticate_user() method. love call in before(:each) , make sure setting correct session key.

so created file called /spec/routing/auth_routing_spec.rb , tried follow guide @ http://relishapp.com/rspec/rspec-rails/v/2-6-rc/dir/routing-specs. created following spec:

it "/ should send me home page"   get('/').should route_to(:controller => :index) end 

when run spec run error on route has :constraint. assuming env['rack.session'] not exists. tried mock request object out via rails.application.call(rack::mockrequest.env_for('/')) did not out.

failure/error: get('/').should route_to(:controller => :index) nomethoderror:   undefined method `has_key?' nil:nilclass 


boring app details

rake about yields

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 yields

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! 

i in controller specs, maybe same approach work here?

describe "some controller"   "does something"     request.stub(:env => {'rack.session' => {'current_user' => '42'})     :my_action     response.should be_cool   end end 

upon reflection, better approach probably:

class currentuserconstraint   def self.matches?(request)     request.session[:current_user_id].present?   end end 

and:

root :to => 'intentions#latest', :constraints => currentuserconstraint.new 

and have logic in class testable.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -