34

在学习 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"}

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

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

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

它失败并出现错误:

No route matches {:action => "index", :controller => "domains"}

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

如何修改它domains_controller_test.rb以便理解嵌套资源?

4

3 回答 3

47

将 customer_id 与请求一起传递就可以了。像这样的东西:-

类 DomainsControllerTest < ActionController::TestCase
  测试“应该得到索引”做
    得到 :index ,:customer_id=> 1
    断言响应:成功
    assert_not_nil 分配(:域)
  结尾
结尾
于 2009-09-20T15:28:08.520 回答
1

根据您的路线,域不再存在于客户上下文之外。该请求需要一个customer_id用于匹配的命名路由。

在您的测试中执行此操作的方法是:

test "should get index" do
  get :index, :customer_id=>joe
end
于 2009-09-20T15:41:28.413 回答
1

有一个叫做nester的 gem可以解决这个确切的问题。它生成的方法可以返回 adomains_path和类似的方法,就好像您的路线没有嵌套一样。例如,当您的视图和测试引用 时,domain_path(domain)路由将扩展为customer_domain_path(domain.customer, domain)。还有一个ActionController::TestCase助手可以为,等:customer_id => @domain.customer方法添加。getpost

默认生成的功能测试和视图使用这种方法开箱即用。(免责声明:我写的)。

于 2011-10-07T01:35:45.810 回答