【问题标题】:Route test failing on: NoMethodError: undefined method `values' for "/the/route":String路由测试失败:NoMethodError: undefined method `values' for "/the/route":String
【发布时间】:2012-02-05 22:48:02
【问题描述】:

我遇到了一个失败的测试,我在调试时遇到了很大的困难。

我的测试用例是:

it "routes to #active" do
  get ("/parties/active").should route_to("parties#active")
end

我的路线是:

resources :parties do
  get 'active', :on => :collection
end

当我运行测试用例时,我收到以下错误:

NoMethodError: undefined method `values' for "/parties/active":String

完整的错误输出在:https://gist.github.com/1748264

当我运行 rake 路线时,我看到:

active_parties GET    /parties/active(.:format)   parties#active
       parties GET    /parties(.:format)          parties#index
               POST   /parties(.:format)          parties#create
     new_party GET    /parties/new(.:format)      parties#new
    edit_party GET    /parties/:id/edit(.:format) parties#edit
         party GET    /parties/:id(.:format)      parties#show
               PUT    /parties/:id(.:format)      parties#update
               DELETE /parties/:id(.:format)      parties#destroy
          root        /                           parties#show

我正在运行的测试实际上与脚手架生成的新测试相同:

it "routes to #new" do
  get("/parties/new").should route_to("parties#new")
end

知道发生了什么吗?

【问题讨论】:

  • 我还发现在before 块中使用get 会导致同样的错误。

标签: ruby-on-rails rspec-rails rails-routing


【解决方案1】:

您在 get 和方法的参数之间放置了一个空格。不要那样做。

你有这个:

it "routes to #active" do
  get ("/parties/active").should route_to("parties#active")
end

什么时候应该是这样的:

it "routes to #active" do
  get("/parties/active").should route_to("parties#active")
end

否则它将像这样评估:

  get(("/parties/active").should route_to("parties#active"))

【讨论】:

    【解决方案2】:

    尝试使用散列式路由:

    { :get => 'parties/new' }.should route_to('parties#new')
    

    【讨论】:

    • Brandan,您的解决方案有效。 :-) 如果您对我的代码失败的原因感兴趣,那是由于 get 和左括号之间的空格,正如 Ryan Bigg 上面指出的那样。
    猜你喜欢
    • 1970-01-01
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-01
    相关资源
    最近更新 更多