Specifying the default format in the routing specification

I have the following rspec routing specification, but I need to specify :defaults => { :format => 'json' } in the message; how would i do that?

Specification:

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

change # 1
therefore, playing around seems like this fix:

 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 somewhere?

+7
source share
1 answer

Just set the format to specification like this ...

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

Long explanation ...

The behavior you see is not specific to :format , but rather is the relationship between the characters you see in rake routes and the characters you pass to route_to .

For example, given your example above, I expect when running rake routes :

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

:controller and :action are not explicitly marked in the rake routes answer, since they are embedded in the MVC Rails structure, but :format displayed explicitly, and the :format pass to route_to . For example...

Similarly, you will probably see several links :id on the output of rake routes , which will be used when passing the parameter :id to route_to .

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

Inside RSpec, route_to delegates the Rails' assert_recognizes , which you can see in the Rails documentation.

+4
source

All Articles