How to simulate a database crash for testing purposes (in Ruby on Rails)

This is a common setting in order to survive in the application with a heartbeat message by some monitoring tool, for example Monit. If the application is launched and everything works correctly, it returns the message "I'm alive", if the database fails or the web server freezes, it does not return anything or an internal server error (HTTP status code 500). How can you simulate a database failure to test this behavior in Ruby on Rails? It would be nice if you could enable / disable this function for testing in the test itself ( Test::Unit or RSpec ).

+4
source share
1 answer

It looks like you can use ActiveRecord::Base.remove_connection to simulate a database failure. Using RSpec, it would look like this:

  describe "GET running" do it "renders a 500 if crashed" do ActiveRecord::Base.remove_connection get :running response.response_code.should == 500 ActiveRecord::Base.establish_connection end end 
+5
source

Source: https://habr.com/ru/post/1380785/


All Articles