Reputation: 953
I'm using a Rails 5.1 environment. I have a script located at
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?
Upvotes: 1
Views: 1164
Reputation: 434945
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
:
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).
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.
Upvotes: 2
Reputation: 5459
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
Upvotes: -1