我正在尝试干掉我的访问控制测试,因为我会为每个操作调用相同的访问控制测试。
我的程序为每个帐户创建一个单独的子域。当子域'foo'的登录用户尝试访问子域'bar'上的页面时,他们会被重定向到子域'foo'上的同一页面。
以下是典型的访问控制测试:
test "on admin page, logged-in user from other subdomain redirects" do
# log-in the user and other setup here...
get :admin
assert_redirected_to admin_url(subdomain: other_users_subdomain)
end
工作得很好,但是我想通过这样的方式停止复制并粘贴每一个动作:
["all", "my", "action", "names", "go", "here"].each do |method_name|
test "on #{method_name} page, logged-in user from other subdomain redirects" do
# log-in the user and other setup here...
get method_name.to_sym
assert_redirected_to send("#{method_name}_url".to_sym, subdomain: other_users_subdomain)
end
end
但是,由于send
,这不起作用。对于每个测试,我收到如下错误消息:
NoMethodError: undefined method 'admin_url' for #<ShopsControllerTest:0x0000010643fbf0>
我一直试图找到确切的* _url助手是什么方法,所以我可以使这个工作,但到目前为止没有骰子。有什么建议吗?
编辑:我仍然希望让* _url帮助程序正常工作,但解决办法就是这样调用assert_redirected_to
:
assert_redirected_to action: method_name, subdomain: other_users_subdomain