ruby-on-rails – 指定路由规范中的默认格式
我有以下rspec路由规范,但我需要指定:defaults => {:format => ‘json’}在帖子中;我该怎么做?
规格: it "should route to all-locations-nav" do {:post => locations_nav_path }.should route_to(:controller => "api",:action => "locations_nav") end 编辑#1 it "should route to all-locations-nav" do {:post => locations_nav_path }.should route_to(:controller => "api",:action => "locations_nav",:format => "json") end 但如果在任何地方记录这一点,我很好奇吗? 解决方法
只需在规范中设置格式,就像这样……
it "routes to #create" do expect(:post => "/post").to route_to("posts#create",:format => :json) end 很长的解释…… 您所看到的行为并非特定于:format,而是您在rake路由中看到的符号与传递给route_to的符号之间的关系. 例如,根据上面的示例,当您运行rake路由时,我会期望类似以下内容: locations_nav POST /api/locations_nav(.:format)api#locations_nav :rake路由响应中没有显式标记:controller和:action,因为它们内置在Rails的MVC结构中,但是:format显式显示,并将:format格式传递给route_to.例如 … 类似地,您可能会在rake路由输出中看到一些:id引用,这可以通过将:id参数传递给route_to来实现. 关于RSpec中的路由的一些其他示例可以在“rspec-rails” documentation中看到. 在内部,RSpec的route_to委托给Rails的assert_recognizes,你可以在Rails documentation中看到它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |