【问题标题】:ActionController::UrlGenerationError: error No routes matchesActionController::UrlGenerationError: 错误没有路由匹配
【发布时间】:2014-08-08 06:09:58
【问题描述】:

我收到以下错误 ActionController::UrlGenerationError。

ActionController::UrlGenerationError:
       No route matches {:action=>"/accounts/100", :controller=>"accounts"}

以下是引发此错误的代码。

  it "can find an account" do
  Account.should_receive(:find, with: {id: 2055, authorization: @auth}

  get "/accounts/100", nil, {"AUTH_TOKEN" => @auth}

  hashed_response = {
    "@type" => "test",
    "createdAt" => "2014-07-24T15:26:49",
    "description" => "Something about test",
    "disabled" => false
  }
  expect(response.status).to eq 200    
  expect(response.body).to eq(hashed_response.to_json);

end

我对此进行了谷歌搜索,发现没有为此定义路由。以下是我的config/routes.rb 文件

Rails.application.routes.draw do
   resources :accounts do
      resources :holders
   end
end

【问题讨论】:

  • 为什么100 是硬编码的?
  • 执行rake routes,您可以看到应用程序可用的路由模式。

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 rspec


【解决方案1】:

我认为此错误来自控制器规范?如果是这样,您只需使用表示您要调用的操作的符号,而不是 URL 本身。

例如。这是一个表演动作,所以你可以使用:

get :show, id: 100

【讨论】:

  • 是的,因此您正在使用控制器规范测试控制器操作。此外,您使用错误的 ID 存根帐户,并且在同一行有语法错误。
  • 并且您正在查找帐户 ID 2055
  • 但是我的 show 方法输出的是 html 而不是我需要比较的 json。
  • 失败/错误:expect(response.body).to eq(hashed_response.to_json);预期:"{\"@type\":\"test\",\"createdAt\":\"2014-08-07T14:31:58.198\",\"createdBy\":2,\"updatedAt\" :\"2014-08-07T14:31:58.247\",\"updatedBy\":2,\"accountid\":2055,\"name\":\"test\",\"description\":\ "Something about test\",\"disabled\":false}" got: "你正在被test.host/login\">重定向。 "
  • 您的操作需要身份验证,而您没有在测试中进行身份验证。你也没有请求 JSON,所以如果你得到 JSON,我会很惊讶。
猜你喜欢
  • 1970-01-01
  • 2015-11-26
  • 1970-01-01
  • 2019-05-25
  • 1970-01-01
  • 1970-01-01
  • 2016-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多