路线在 Rspec 中不起作用

Route Not Working in Rspec

鉴于遵循 rails 路线 ...

post '/a/link.aspx', to: 'vendor_simulator#an_action'

路线显示为...

Prefix Verb URI Pattern                              Controller#Action
       POST /a/link.aspx(.:format) vendor_simulator#an_action

并通过以下测试...

it "returns http success" do
  post :an_action, return_xml, :content_type => 'application/xml'
  expect(response).to have_http_status(:success)
end

为什么下面的测试失败了...

it "returns http success" do
  post "/a/link.aspx", return_xml, :content_type => 'application/xml'
  expect(response).to have_http_status(:success)
end

有...

  1) VendorSimulatorController POST '/a/link' returns http success
     Failure/Error: post "/a/link.aspx", return_xml, :content_type => 'application/xml'
     ActionController::UrlGenerationError:
       No route matches {:action=>"/a/link.aspx", :content_type=>"application/xml", :controller=>"vendor_simulator"}

您似乎有一个控制器测试(而不是集成测试)。如果是这种情况,那么第二个失败,因为控制器测试绕过路由,而是需要被告知使用 post(或获取或放置或删除)调用哪个操作方法,而不是被给予 URL 路径。