Harm
2009-Mar-18 12:03 UTC
[rspec-users] Speccing module which should be included in controller
Dear all, I have a module ApiAccessControl which is included in all controllers which deal with the API. It sees if the users is valid, logs etc etc. I want to see if this module does what it is supposed to but I dont want to test that in every single controller dealing with the API. So I created a file api_access_control_spec.rb with this content: require File.dirname(__FILE__) + ''/../spec_helper'' class ApiAccessControlTestController < ApplicationController include ApiAccessControl end describe ApiAccessControlTestController do describe "without valid credentials" do it "should respond with a 401 Unauthorized on the GET ''index''" do get "index" response.response_code.should == 401 end end end This works by the grace that my routes file has a fallback map.connect '':controller/:action/:id''. I never use that fallback only for this particular test. Is there anyway to get rid of this entry in my routes.rb? I tried something like this is a before block: ActionController::Routing::RouteSet.new.add_route("index", :controller => "api_access_control_test", :action => "index") But that doesn''t seem to work. How do you guys test this sort of stuff? Harm
Matt Wynne
2009-Mar-18 16:16 UTC
[rspec-users] Speccing module which should be included in controller
On 18 Mar 2009, at 12:03, Harm wrote:> Dear all, > > I have a module ApiAccessControl which is included in all controllers > which deal with the API. It sees if the users is valid, logs etc etc. > > I want to see if this module does what it is supposed to but I dont > want to test that in every single controller dealing with the API. So > I created a file api_access_control_spec.rb with this content: > > require File.dirname(__FILE__) + ''/../spec_helper'' > > class ApiAccessControlTestController < ApplicationController > include ApiAccessControl > end > > describe ApiAccessControlTestController do > describe "without valid credentials" do > it "should respond with a 401 Unauthorized on the GET ''index''" do > get "index" > response.response_code.should == 401 > end > end > end > > This works by the grace that my routes file has a fallback map.connect > '':controller/:action/:id''. I never use that fallback only for this > particular test. Is there anyway to get rid of this entry in my > routes.rb? > > I tried something like this is a before block: > ActionController::Routing::RouteSet.new.add_route("index", :controller > => "api_access_control_test", :action => "index") > > But that doesn''t seem to work. How do you guys test this sort of > stuff? > > HarmSee this thread: http://www.nabble.com/How-to-Spec-a-Module-to-be-used-in-a-Controller-td20128726.html It''s slow though. Matt Wynne http://blog.mattwynne.net http://www.songkick.com
Pat Maddox
2009-Mar-18 16:57 UTC
[rspec-users] Speccing module which should be included in controller
On Mar 18, 2009, at 5:03 AM, Harm wrote:> This works by the grace that my routes file has a fallback map.connect > '':controller/:action/:id''. I never use that fallback only for this > particular test. Is there anyway to get rid of this entry in my > routes.rb?map.connect '':controller/:action/:id'' if RAILS_ENV == ''test'' yahtzee