Reputation: 3
When run rake test
, I get this error: ActionController::UrlGenerationError
my routes.rb
resources :posts do
resources :comments, only: [ :create,:destroy ]
end
comments.yml
one:
body: "comment one"
user: users(:paul)
post: posts(:one)
comments_controller_test.rb
def setup
@comment = comments(:one)
end
test "should redierct create when not signed in" do
assert_no_difference "Comment.count" do
post :create, comment: @comment.attributes
end
assert_redirected_to signin_url
end
comments_controller.rb
def create
@comment = @post.comments.build(comment_params.merge(user: current_user))
@comment.save
@comments = @post.comments.all
respond_to do |format|
format.html { redirect_to @post }
format.js
end
end
Complete error message
ERROR["test_should_redierct_create_when_not_signed_in", CommentsControllerTest, 0.444971] test_should_redierct_create_when_not_signed_in#CommentsControllerTest (0.44s) ActionController::UrlGenerationError:
ActionController::UrlGenerationError: No route matches {:action=>"create", :comment=>{"id"=>"980190962", "body"=>"comment one", "post_id"=>"849819558", "user_id"=>"293831562", "created_at"=>"2014-11-16 10:45:03 UTC", "updated_at"=>"2014-11-16 10:45:03 UTC"}, :controller=>"comments"}
How should I modify my test code?
Upvotes: 0
Views: 125
Reputation: 5839
I think you need to explicitly tell about the parent record id (post_id) so rails can form the correct post path, try something like:
post :create, comment: @comment.attributes, post_id: @comment.post_id
Upvotes: 0