【发布时间】:2015-10-12 01:31:45
【问题描述】:
我正在自学编写控制器测试,却遇到了这个错误:
ERROR["test_should_update_post", PostsControllerTest, 2015-10-11 12:12:31 -0400]
test_should_update_post#PostsControllerTest (1444579951.69s)
ActionController::UrlGenerationError: ActionController::UrlGenerationError: No route matches {:action=>"update", :controller=>"posts", :post=>{:title=>"My Post", :body=>"Updated Ipsum"}}
test/controllers/posts_controller_test.rb:51:in `block (2 levels) in <class:PostsControllerTest>'
test/controllers/posts_controller_test.rb:50:in `block in <class:PostsControllerTest>’
这是我的测试:
test "should update post" do
assert_difference('Post.count') do
put :update, post: {title: 'My Post', body: 'Updated Ipsum'}
end
assert_redirected_to post_path(assigns(:post))
end
这是我的 yaml:
entry_one:
title: "Foobar"
body: "Ipsum This"
entry_two:
title: "Barfoo"
body: "This Ipsum"
这是我的控制器:
def update
@post = Post.find(params[:id])
if @post.update(post_params)
redirect_to @post, notice: 'Event updated successfully'
else
render :edit
end
end
你能指出我需要解决的问题吗?
我可以从错误和行数中看出它与行有关:
assert_difference('Post.count') do 和 put :update, post: {title: 'My Post', body: 'Updated Ipsum’}
【问题讨论】: