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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With