Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simulate database failure for test purposes (in Ruby on Rails)

It is a common setup to survise an application with a heartbeat message by some monitoring tool, for example Monit. If the application is running and everything is working correctly, it returns an "I am alive" message, if the database fails or the web server hangs it returns nothing or an internal server error (HTTP status code 500) page. How can you simulate a database failure to test this behavior in Ruby on Rails? It would be nice if one could enable/disable this feature for test purposes within the test (Test::Unit or RSpec) itself.

like image 301
0x4a6f4672 Avatar asked Nov 11 '11 15:11

0x4a6f4672


1 Answers

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

  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
like image 152
0x4a6f4672 Avatar answered Sep 16 '22 18:09

0x4a6f4672