我有一个表单,应该是 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/