将 rails 预加载路径中的子文件夹列入黑名单
Blacklist a subfolder in rails eager load path
我需要将来自每个环境的 api 命名空间中的路由列入黑名单,但测试除外。原因是,我们的集成套件与一些使用 API 的微服务需要在测试运行之间擦除 API 数据库。
假设我有以下应用程序目录结构。
app
\_controllers
\_api
\_v1
\_test
\_test_controller.rb
base_controller.rb
some_controller.rb
如何将 controllers/api/v1/test/
列入黑名单,但仍将 app/controllers
保留在急切加载路径中?
编辑:
# routes.rb
namespace :api do
namespace :v1 do
resources :instruments, only: [:index, :show]
resources :instrument_roots, only: [:index, :show]
if Rails.env.test? # integration suite handlers
namespace :test do
resources :instruments, only: [:destroy]
post '/reset' => 'devise/sessions#destroy'
end
end
end
end
我正在通过环境检查删除路由,但理想情况下我根本不会加载控制器。
删除 app/controllers
的子目录不太可能,因为各个目录条目首先不存在于加载路径中。
您可能会通过创建 RAILS_ROOT/test_api_support
(这又会有一个 app/controllers
目录)并将这些文件移动到那里来获得结果。然后,您可以将该目录添加到仅 spec_helper.rb
、test_helper.rb
或 config/environments/test.rb
.
中的加载路径
这种方法应该具有将它们从其他环境中的 VM 内存中移除的效果。
我需要将来自每个环境的 api 命名空间中的路由列入黑名单,但测试除外。原因是,我们的集成套件与一些使用 API 的微服务需要在测试运行之间擦除 API 数据库。
假设我有以下应用程序目录结构。
app
\_controllers
\_api
\_v1
\_test
\_test_controller.rb
base_controller.rb
some_controller.rb
如何将 controllers/api/v1/test/
列入黑名单,但仍将 app/controllers
保留在急切加载路径中?
编辑:
# routes.rb
namespace :api do
namespace :v1 do
resources :instruments, only: [:index, :show]
resources :instrument_roots, only: [:index, :show]
if Rails.env.test? # integration suite handlers
namespace :test do
resources :instruments, only: [:destroy]
post '/reset' => 'devise/sessions#destroy'
end
end
end
end
我正在通过环境检查删除路由,但理想情况下我根本不会加载控制器。
删除 app/controllers
的子目录不太可能,因为各个目录条目首先不存在于加载路径中。
您可能会通过创建 RAILS_ROOT/test_api_support
(这又会有一个 app/controllers
目录)并将这些文件移动到那里来获得结果。然后,您可以将该目录添加到仅 spec_helper.rb
、test_helper.rb
或 config/environments/test.rb
.
这种方法应该具有将它们从其他环境中的 VM 内存中移除的效果。