[英]Rails 4 minitest functional test failed for create method
Here's my Code for controller 这是我的控制器代码
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
and my code for testing 和我的测试代码
test "should get create post" do
assert_difference('Post.count') do
post :create, post: {title: @post.title, content: @post.content}
end
assert_redirected_to post_path(assigns{:post})
end
The error that I'm getting is 我得到的错误是
ArgumentError: comparison of Array with Array failed test/controllers/posts_controller_test.rb:22:in `block in ' ArgumentError:将Array与Array比较失败test / controllers / posts_controller_test.rb:22:in'block in'
if i remove (assigns{:post}) and try the test 如果我删除(assigns {:post})并尝试测试
i get this error 我得到这个错误
ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"posts"} missing required keys: [:id] Also how to test the if fails render 'new' part in controller. ActionController :: UrlGenerationError:没有路由匹配{:action =>“ show”,:controller =>“ posts”}缺少必需的键:[:id]同样如何测试if是否在控制器中渲染'new'部分。
Thanks 谢谢
In the following line 在下一行
assert_redirected_to post_path(assigns{:post})
use parenthesis instead of curly braces. 使用括号代替大括号。 The code should look like this:
该代码应如下所示:
assert_redirected_to post_path(assigns(:post))
@post
is not instantiated anywhere in the test. @post
不会在测试中的任何地方实例化。 You need to pass in params like so 你需要像这样传递参数
post :create, post: {title: 'title', content: 'content'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.