ruby-on-rails-3 - 在 Rails 3 中使用 Rspec 测试路由

标签 ruby-on-rails-3 rspec routes

我有一个表单,应该是 POST to CREATE 操作,但它正在执行 POST to INDEX 操作。所以我决定用 rspec 测试我的路线。在我的示例中,我的测试如下。

it "should recognize a specific invoices#create route" do
  assert_routing("/invoices", {:controller => "invoices", :action => "create"})
end

但是当我运行测试时,它出现了这个错误。

1) InvoicesController on get to :index should recognize a specific invoices#create route
 Failure/Error: assert_routing("/invoices", {:controller => "invoices", :action => "create"})
 The recognized options <{"action"=>"index", "controller"=>"invoices"}> did not match <{"controller"=>"invoices", "action"=>"create"}>, difference: <{"action"=>"create"}>.
 Expected block to return true value.

所以我试图弄清楚为什么我的表单在 INDEX 上执行 POST 以及为什么我的测试认为我在执行索引路由。我尝试在测试中插入 :method => :post 但似乎不起作用。

最佳答案

你试过这个吗?:

assert_routing({ :path => "invoices", :method => :post }, 
  { :controller => "invoices", :action => "create" })

关于ruby-on-rails-3 - 在 Rails 3 中使用 Rspec 测试路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5099518/

相关文章:

ruby-on-rails - Devise Rails 3.1.3 没有路线匹配 [DELETE] "/users/sign_out"

ruby-on-rails - Rails 4 内存分析

ruby-on-rails - 在 rspec 中 stub 接受 block 的方法调用

ruby-on-rails - Cookie不会在Rails 3.1的Rspec中持续存在

php - 如何将变量传递给中间件函数? [SLIM框架]

ruby-on-rails - Rails 新手,是否有分类/排序/评论 ruby​​gems/插件等的网站?

ruby - 在 Ruby on Rails 中验证 slug 时排除方法名称?

java - Play Framework 2.3.4 路由重定向到子项目

c# - 为什么 MapPoint.Route.Calculate() 比应用程序本身的计算慢得多?

json - 导轨 3 : Create a valid JSON Object from an Array of data