Otis Harrison
2011-Jul-14 22:00 UTC
RoutingError with RSpec Controller test on Scoped Route
So I have a route that looks like this:
scope "4" do
scope "public" do
scope ":apikey" do
resources :shops
end
end
end
And a bunch of controller specs, an example of which looks like this:
describe ShopsController do
describe "when responding to a GET" do
context "#new" do
it "should create a new instance of the shop class" do
get :new
@shop.should_not_be_nil
end
end
end
end
In rake routes, as well as via a web browser, this controller/action
works fine. However, RSpec throws:
1) ShopsController when responding to a GET#new should create a new
instance of the shop class
Failure/Error: get :new
ActionController::RoutingError:
No route matches {:controller=>"shops",
:action=>"new"}
# ./spec/controllers/shops_controller_spec.rb:9:in `block (4 levels)
in <top (required)>''
When I remove the scope statements from the route, the tests work
fine. Is there a way to "inform" RSpec of the route scopes?
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
David Chelimsky
2011-Jul-16 12:11 UTC
Re: RoutingError with RSpec Controller test on Scoped Route
On Jul 14, 2011, at 5:00 PM, Otis Harrison wrote:> So I have a route that looks like this: > > > scope "4" do > scope "public" do > scope ":apikey" do > resources :shops > end > end > end > > > And a bunch of controller specs, an example of which looks like this: > > > describe ShopsController do > > describe "when responding to a GET" do > > context "#new" do > it "should create a new instance of the shop class" do > get :new > @shop.should_not_be_nil > end > end > > end > > end > > In rake routes, as well as via a web browser, this controller/action > works fine. However, RSpec throws: > > 1) ShopsController when responding to a GET#new should create a new > instance of the shop class > Failure/Error: get :new > ActionController::RoutingError: > No route matches {:controller=>"shops", :action=>"new"} > # ./spec/controllers/shops_controller_spec.rb:9:in `block (4 levels) > in <top (required)>'' > > When I remove the scope statements from the route, the tests work > fine. Is there a way to "inform" RSpec of the route scopes?The route requires an api key, so that needs to be included in the args passed to ''get'': get :new, :apikey => "ignore" As for @shop.should_not be_nil, that''s probably going to fail as well once you get past the route issue. To specify that a new instance gets created, you''ll need to either interact with the database, e.g. expect do get :new, :apikey => "ignore" end.to change(Shop, :count).by(1) ... or mock the message to create the model: Shop.should_receive(:create) get :new, :apikey => "ignore" HTH, David -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.