如何在执行测试时阻止初始化程序脚本运行?

时间:2023-01-18 19:22:46

I'm using a Rails 5.1 environment. I have a script located at

我正在使用Rails 5.1环境。我有一个脚本位于

config/initializers/websockets.rb

This is run when my server starts, however, I don't want this script to run when I execute tests, for instance ...

这是在我的服务器启动时运行的,但是,我不希望在执行测试时运行此脚本,例如......

localhost:mine satishp$ rails test test/lib/websocket_client_test.rb

How do I prevent the initializers scripts from running when I'm executing my tests, or at least this specific file?

在执行测试时,或者至少在此特定文件中,如何阻止初始化程序脚本运行?

2 个解决方案

#1


1  

Initializers are just Ruby scripts that are executed when Rails is starting up so you can put whatever code you want in them. In particular, you can look at Rails.env:

初始化器只是在Rails启动时执行的Ruby脚本,因此您可以在其中放置所需的任何代码。特别是,您可以查看Rails.env:

if(!Rails.env.test?)
  # Do whatever needs to be done...
end

or environment variables:

或环境变量:

if(!ENV['SUPPRESS_WEB_SOCKET_INIT'])
   #...
end

and then set the SUPPRESS_WEB_SOCKET_INIT environment variable when you run your tests (or reverse the logic if that's a better fit).

然后在运行测试时设置SUPPRESS_WEB_SOCKET_INIT环境变量(如果更合适,则反转逻辑)。

These are blunt instruments though so you might want to reconsider your approach. Perhaps it would make more sense to initialize your websocket stuff the first time it is needed and then set up some mocks to replace the functionality in your tests.

这些都是钝器,所以你可能想重新考虑你的方法。也许在第一次需要时初始化websocket东西更有意义,然后设置一些模拟来替换测试中的功能。

#2


0  

Move the initializer script out of the directory, then move it back afterward.

将初始化程序脚本移出目录,然后再将其移回。

localhost:mine satishp$ mv $RUBY_DIRECTORY/config/initializers/websockets.rb /home/satishp && \
rails test test/lib/websocket_client_test.rb && \
cp /home/satishp/websockets.rb $RUBY_DIRECTORY/config/initializers/websockets.rb

#1


1  

Initializers are just Ruby scripts that are executed when Rails is starting up so you can put whatever code you want in them. In particular, you can look at Rails.env:

初始化器只是在Rails启动时执行的Ruby脚本,因此您可以在其中放置所需的任何代码。特别是,您可以查看Rails.env:

if(!Rails.env.test?)
  # Do whatever needs to be done...
end

or environment variables:

或环境变量:

if(!ENV['SUPPRESS_WEB_SOCKET_INIT'])
   #...
end

and then set the SUPPRESS_WEB_SOCKET_INIT environment variable when you run your tests (or reverse the logic if that's a better fit).

然后在运行测试时设置SUPPRESS_WEB_SOCKET_INIT环境变量(如果更合适,则反转逻辑)。

These are blunt instruments though so you might want to reconsider your approach. Perhaps it would make more sense to initialize your websocket stuff the first time it is needed and then set up some mocks to replace the functionality in your tests.

这些都是钝器,所以你可能想重新考虑你的方法。也许在第一次需要时初始化websocket东西更有意义,然后设置一些模拟来替换测试中的功能。

#2


0  

Move the initializer script out of the directory, then move it back afterward.

将初始化程序脚本移出目录,然后再将其移回。

localhost:mine satishp$ mv $RUBY_DIRECTORY/config/initializers/websockets.rb /home/satishp && \
rails test test/lib/websocket_client_test.rb && \
cp /home/satishp/websockets.rb $RUBY_DIRECTORY/config/initializers/websockets.rb