gpt4 book ai didi

ruby-on-rails - Rspec redirect_to 显示操作

转载 作者:行者123 更新时间:2023-12-05 00:03:14 29 4
gpt4 key购买 nike

我正在学习 Rails 并且遇到了 Rspec 的问题。我对 Controller 进行了以下测试:

describe PostsController, "creating a new post" do
it "should redirect and show the post" do
Post.stub!(:save).and_return(true)
post "create"
response.should redirect_to :action => "show"
end
end

当我运行此测试时,出现以下故障:

PostsController creating a new post should redirect and show the post
Failure/Error: response.should redirect_to :action => "show"
ActionController::RoutingError:
No route matches {:action=>"show", :controller=>"posts"}
# ./spec/controllers/posts_controller_spec.rb:8:in `block (2 levels) in <top (required)>'

然而,当我检查我的路线时,我看到我的帖子 Controller 显示 Action :

post GET    /posts/:id(.:format)      {:action=>"show", :controller=>"posts"}

我可能遗漏了一些非常简单但很难找到的东西。

谢谢。

最佳答案

您忘记了 id

我经常写

response.should redirect_to(post_path(assigns[:post])

关于ruby-on-rails - Rspec redirect_to 显示操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6139898/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com