Rspec redirect_to显示操作

sta*_*ona 2 ruby-on-rails rspec2

我正在学习rails并且遇到了Rspec的问题.我对控制器进行了以下测试:

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
Run Code Online (Sandbox Code Playgroud)

当我运行此测试时,我遇到以下故障:

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)>'
Run Code Online (Sandbox Code Playgroud)

然而,当我检查我的路线时,我看到我的帖子控制器的显示动作:

post GET    /posts/:id(.:format)      {:action=>"show", :controller=>"posts"}
Run Code Online (Sandbox Code Playgroud)

我可能错过了一些非常简单但很难找到它的东西.

谢谢.

Tho*_*oll 11

你忘记了id.

我经常写

response.should redirect_to(post_path(assigns[:post])
Run Code Online (Sandbox Code Playgroud)