使用 RSpec 控制器在范围路由上进行测试时出现 RoutingError
所以我有一条如下所示的路线:
scope "4" do
scope "public" do
scope ":apikey" do
resources :shops
end
end
end
以及一堆控制器规格,其中一个示例如下所示:
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
在 rake 路线中,以及通过网络浏览器,此控制器/操作工作正常。但是,RSpec 抛出:
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)>'
当我从路由中删除 scope
语句时,测试工作正常。有没有办法“告知”RSpec 路由范围?
提前致谢!
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?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据您的路线,
:apikey
是必需参数,因此您需要将其添加到您的 get 中:此外,您还应该将下一行中的期望更改为:
Use
assigns
检查控制器的实例变量,并使用空格而不是下划线将should_not
与匹配器分开。最后一点需要一些时间来适应。According to your routes,
:apikey
is a required parameter, so you need to add it to your get:Also you should change the expectation in your next line to this:
Use
assigns
to check the controller's instance variables, and separateshould_not
from the matcher with a space, not an underscore. That last bit takes some getting used to.