Rails:如何修改嵌套资源的测试?

Mar*_*els 34 testing routing nested ruby-on-rails

在学习Rails时,我创建了一个应用程序,其Domains控制器嵌套在Customers控制器下面.我正在使用Rails 2.3.4,这是一次学习经历.我设法得到以下路由设置:

    customer_domains GET    /customers/:customer_id/domains(.:format)          {:controller=>"domains", :action=>"index"}
                     POST   /customers/:customer_id/domains(.:format)          {:controller=>"domains", :action=>"create"}
 new_customer_domain GET    /customers/:customer_id/domains/new(.:format)      {:controller=>"domains", :action=>"new"}
edit_customer_domain GET    /customers/:customer_id/domains/:id/edit(.:format) {:controller=>"domains", :action=>"edit"}
     customer_domain GET    /customers/:customer_id/domains/:id(.:format)      {:controller=>"domains", :action=>"show"}
                     PUT    /customers/:customer_id/domains/:id(.:format)      {:controller=>"domains", :action=>"update"}
                     DELETE /customers/:customer_id/domains/:id(.:format)      {:controller=>"domains", :action=>"destroy"}
           customers GET    /customers(.:format)                               {:controller=>"customers", :action=>"index"}
                     POST   /customers(.:format)                               {:controller=>"customers", :action=>"create"}
        new_customer GET    /customers/new(.:format)                           {:controller=>"customers", :action=>"new"}
       edit_customer GET    /customers/:id/edit(.:format)                      {:controller=>"customers", :action=>"edit"}
            customer GET    /customers/:id(.:format)                           {:controller=>"customers", :action=>"show"}
                     PUT    /customers/:id(.:format)                           {:controller=>"customers", :action=>"update"}
                     DELETE /customers/:id(.:format)                           {:controller=>"customers", :action=>"destroy"}
                root        /                                                  {:controller=>"customers", :action=>"index"}
Run Code Online (Sandbox Code Playgroud)

但是,由于路由错误,域控制器的所有测试都会失败.

例如,以下测试(由Rails的资源生成器生成)失败,DomainsControllerTest类中的所有其他测试也失败.

class DomainsControllerTest < ActionController::TestCase
  test "should get index" do
    get :index
    assert_response :success
    assert_not_nil assigns(:domains)
  end
end
Run Code Online (Sandbox Code Playgroud)

它失败并出现错误:

No route matches {:action => "index", :controller => "domains"}
Run Code Online (Sandbox Code Playgroud)

这是有道理的,因为默认路由不再存在,并且域控制器需要@customer设置a.我花了一个下午寻找所需的更改,但几乎每个站点都在谈论Rspec测试而不是常规的Rails测试.

如何修改,domains_controller_test.rb以便了解嵌套资源?

Ris*_*ogi 47

将customer_id与请求一起传递即可.像这样: -

class DomainsControllerTest < ActionController::TestCase
  test "should get index" do
    get :index ,:customer_id=> 1
    assert_response :success
    assert_not_nil assigns(:domains)
  end
end