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 remov开发者_StackOverflow社区e the scope
statements from the route, the tests work fine. Is there a way to "inform" RSpec of the route scopes?
Thanks in advance!
According to your routes, :apikey
is a required parameter, so you need to add it to your get:
get :new, :apikey => "something"
Also you should change the expectation in your next line to this:
assigns[:shop].should_not be_nil
Use assigns
to check the controller's instance variables, and separate should_not
from the matcher with a space, not an underscore. That last bit takes some getting used to.
精彩评论