stackjlei
stackjlei

Reputation: 10035

Mismatching route for rspec testing post on a singular resource

I get the error no routes matches even tho in rake routes I have:

... POST /todo/:todo/todo_comments(.:format)

and in my rspec I have

post :todo_comments, params: { todo_id: 1 }

Upvotes: 0

Views: 134

Answers (1)

matthewd
matthewd

Reputation: 4420

It sounds like your routes are defined as:

resources :todos do
  resources :todo_comments
end

In a controller spec (type: :controller), you need to specify the action name in the controller that handles the request. In this case, that's :index:

post :index, params: { todo_id: 1 }

If you're in a request spec (type: :request), on the other hand, it requires the URL to request instead, which you can build using a routing helper:

post todo_todo_comments_path(1)

# or, without the helper:
post "/todos/1/todo_comments"

Note that these forms don't explicitly name the :todo_id parameter, because it will be extracted from the route.

Upvotes: 1

Related Questions