运行rake test
时,我收到此错误: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
完成错误消息
ERROR [" test_should_redierct_create_when_not_signed_in&#34 ;, CommentsControllerTest,0.444971] test_should_redierct_create_when_not_signed_in#CommentsControllerTest (0.44s)ActionController :: UrlGenerationError:
ActionController :: UrlGenerationError:没有路由匹配 {:action =>"创建",:comment => {" id" =>" 980190962"," body" = >"注释 一个"," 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 =>"评论"}
我应该如何修改我的测试代码?
答案 0 :(得分:0)
我认为您需要明确告诉父记录ID(post_id),以便rails可以形成正确的帖子路径,尝试类似:
post :create, comment: @comment.attributes, post_id: @comment.post_id