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

ぐ巨炮叔叔 提交于 2019-12-23 11:39:28

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/8096478/how-to-simulate-database-failure-for-test-purposes-in-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!