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?
我认为您需要明确告知父记录ID(post_id),以便Rails可以形成正确的发布路径,请尝试以下操作:
post :create, comment: @comment.attributes, post_id: @comment.post_id
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.