【发布时间】:2015-01-13 09:19:53
【问题描述】:
运行 rake test 时,出现此错误:ActionController::UrlGenerationError
我的路线.rb
resources :posts do
resources :comments, only: [ :create,:destroy ]
end
cmets.yml
one:
body: "comment one"
user: users(:paul)
post: posts(:one)
cmets_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
cmets_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
完整的错误信息
错误["test_should_redierct_create_when_not_signed_in", 评论控制器测试,0.444971] test_should_redierct_create_when_not_signed_in#CommentsControllerTest (0.44s) ActionController::UrlGenerationError:
ActionController::UrlGenerationError:没有路由匹配 {: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=>"cmets"}
我应该如何修改我的测试代码?
【问题讨论】:
-
不确定,但请尝试将 cmets_controller 中的
redirect_to @post替换为redirect_to [@post,@comments]! -
它不起作用,我认为
post :create, comment: @comment.attributes可能是错误的主要原因。
标签: ruby-on-rails ruby unit-testing