测试多态嵌套资源

时间:2013-07-24 15:22:45

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2

我正在尝试修复多态嵌套资源的默认测试并遇到很多问题。我打赌有一个简单的解决方案,这个新手还没有包裹他的脑袋。任何帮助都将一如既往地受到高度赞赏。

我的模特:

class Member < ActiveRecord::Base
  has_many :names, as: :person
  has_many :dependents

 attr_accessible :active, :deleted
 end

 class Name < ActiveRecord::Base
   belongs_to :person, polymorphic: true

    attr_accessible :dob, :dod, :first, :gender, :last, :mi, :prefix, :relation, :suffix
 end

routes.rb中:

  resources :dependents do
    resources :names 
  end

  resources :members do
   resources :names 
  end

示例测试:

 require 'test_helper'

class NamesControllerTest < ActionController::TestCase
 setup do
  @name = names(:one)
  @person = members(:one)
 end

test "should get edit" do
  get :edit, id: @name
   assert_response :success
 end
 end

我收到错误:

  

3)错误:   test_should_get_edit(NamesControllerTest):   ActionController :: RoutingError:没有路由匹配{:id =&gt;“980190962”,:person_id =&gt;“980190962”,:controller =&gt;“names”,:action =&gt;“edit”}       /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:544:in raise_routing_error' /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:540:in rescue in generate'       /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:532:in generate' /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:573:in generate'       /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:569:in generate_extras' /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_dispatch/routing/route_set.rb:565:in extra_keys'       /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:153:in assign_parameters' /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:465:in process'       /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:49:in process' /usr/local/rvm/gems/ruby-1.9.3-p392/gems/actionpack-3.2.13/lib/action_controller/test_case.rb:392:in get'       /Users/mkenney/hraccess/test/functional/names_controller_test.rb:43:in,block in'

如果我在非嵌套路由中添加回名称,则会运行这些错误。我如何告诉测试它是一个嵌套资源,这是否与多态关系有关,或者只是那些让我失望的噪音?

提前感谢您提供任何帮助,您可以提供这个新手!

标记

1 个答案:

答案 0 :(得分:0)

我需要添加member_id以获得我认为的正确路线:

test "should get edit" do
  get :edit, member_id: @person, id: @name
  assert_response :success
end