Ruby on rails Rails:如何修改嵌套资源的测试?
在学习Rails的过程中,我创建了一个应用程序,其中Ruby on rails Rails:如何修改嵌套资源的测试?,ruby-on-rails,routing,nested,testing,Ruby On Rails,Routing,Nested,Testing,在学习Rails的过程中,我创建了一个应用程序,其中域控制器嵌套在客户控制器下面。我正在使用Rails 2.3.4,这是一次学习经历。我成功地设置了以下路由: customer_domains GET /customers/:customer_id/domains(.:format) {:controller=>"domains", :action=>"index"} POST /customers/:cu
域
控制器嵌套在客户
控制器下面。我正在使用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
这是有意义的,因为默认路由不再存在,并且域控制器需要设置@customer
。我花了一个下午寻找所需的更改,但几乎每个站点都在讨论Rspec测试,而不是常规的Rails测试
如何修改
域\u控制器\u test.rb
,使其能够理解嵌套资源?通过请求传递客户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
类DomainsControllerTest根据您的路线,域不再存在于客户上下文之外。请求需要一个
customer\u id
来匹配指定的路由
在测试中执行此操作的方法是:
test "should get index" do
get :index, :customer_id=>joe
end
有一种叫做gem的方法可以解决这个问题。它会生成一些方法,为您返回一个domains\u路径
,就像您的路由没有嵌套一样。当视图和测试引用(例如,domain\u path(domain)
)时,路由将扩展到customer\u domain\u path(domain.customer,domain)
。还有一个ActionController::TestCase
帮助程序,为get
、post
等方法添加:customer\u id=>@domain.customer
使用这种方法,默认生成的功能测试和视图是开箱即用的。(免责声明:这是我写的)。这似乎行得通,Rishav。有没有什么办法可以把它弄干,而不是把id传给每个单独的测试?Martijn,这就是你要做的。您的路线必须了解客户才能工作。我想你可以编写一个测试助手来替换自动填充散列中客户id部分的“get”调用,但这似乎不是一个好主意,除非你正在编写许多针对DomainsController的测试。我只想指出为什么将其干燥可能是一个坏主意b/c可能很难阅读和调试。干代码很棒,但当它牺牲可读性时就不行了。在测试中,您肯定希望能够看到发生了什么。如果您可以在不牺牲可读性或增加测试脆弱性的情况下使其干燥,那么就这样做吧。我不确定我是否理解这个问题。域创建将是
post:create,:customer\u id=>joe
。customer\u id
仍将在嵌套资源的URL中找到。
test "should get index" do
get :index, :customer_id=>joe
end