0x4a6f4672
0x4a6f4672

Reputation: 28245

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.

Upvotes: 7

Views: 2562

Answers (1)

0x4a6f4672
0x4a6f4672

Reputation: 28245

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

Upvotes: 11

Related Questions