specifying default format in routing spec

I have the following rspec routing spec but I need to specify :defaults => { :format => 'json' } in the post; how would I do this?

spec:

it "should route to all-locations-nav" do
  {:post => locations_nav_path }.should route_to(:controller => "api", :action => "locations_nav")
end

edit #1
so playing around, it looks like this fixes it:

it "should route to all-locations-nav" do
  {:post => locations_nav_path }.should route_to(:controller => "api", :action => "locations_nav", :format => "json")
end

but was curious if this is documented anywhere?


Just set the format in the spec like this ...

it "routes to #create" do
  expect(:post => "/post").to route_to("posts#create", :format => :json)
end

Long explanation ...

The behavior you're seeing is not specific to :format , but is rather a relationship between the symbols you see in rake routes and the symbols that you pass to route_to .

For instance, given your example above, I'd expect something like the following when you run rake routes :

locations_nav POST /api/locations_nav(.:format) api#locations_nav

The :controller and :action aren't explicitly flagged in the rake routes response, as those are built into Rails' MVC structure, but the :format is shown explicitly, and passing the :format to route_to taps into this. For example ...

Similarly, you'll probably see a few :id references in your rake routes output, which would be leveraged by passing an :id parameter to route_to .

Some additional examples around routing in RSpec can be seen in the "rspec-rails" documentation.

Internally, RSpec's route_to delegates to Rails' assert_recognizes , which you can see documented in the Rails documentation.

链接地址: http://www.djcxy.com/p/67490.html

上一篇: 如何在使用状态monad进行遍历时处理嵌套结构

下一篇: 在路由规范中指定默认格式