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

标签 ruby-on-rails rspec2

我正在学习 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/

相关文章:

ruby-on-rails - Rails 4 加密 Cookie 重放攻击

ruby-on-rails - Ruby Sqlite3 安装 sqlite3_libversion_number() macOS Sierra

ruby-on-rails - 一对一关系错误:ActiveModel::MissingAttributeError:无法写入未知属性

ruby-on-rails-4 - Capybara:测试 CSS PsuedoElements 中的内容

testing - rspec - 重定向到 Group.last 去错误的记录

html - 定义列表与缺失定义的对齐

ruby-on-rails - 是否可以在 Rails 应用程序中使用长 id 并将其持久保存到测试数据库?

ruby-on-rails - rails + 设计 : Update user with or without pasword

ruby-on-rails - 在 RSpec 2 中动态生成共享示例?

ruby - rspec: 未定义的方法 'double'