【问题标题】:How to simulate database failure for test purposes (in Ruby on Rails)如何为测试目的模拟数据库故障(在 Ruby on Rails 中)
【发布时间】:2011-11-11 15:48:53
【问题描述】:

通过某些监视工具(例如 Monit)使用心跳消息监视应用程序是一种常见的设置。如果应用程序正在运行并且一切正常,它会返回“我还活着”消息,如果数据库失败或 Web 服务器挂起,它不会返回任何内容或返回内部服务器错误(HTTP 状态代码 500)页面。如何模拟数据库故障以在 Ruby on Rails 中测试这种行为?如果可以在测试(Test::UnitRSpec)本身中启用/禁用此功能以用于测试目的,那就太好了。

【问题讨论】:

    标签: ruby-on-rails database unit-testing testing


    【解决方案1】:

    看起来可以使用ActiveRecord::Base.remove_connection 来模拟数据库故障。使用 RSpec 这看起来像:

      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
    

    【讨论】:

      猜你喜欢
      • 2013-10-08
      • 2010-10-03
      • 2011-10-02
      • 1970-01-01
      • 2010-11-24
      • 2015-06-02
      • 1970-01-01
      • 1970-01-01
      • 2015-11-08
      相关资源
      最近更新 更多