如何模拟数据库失败的测试目的(在Ruby on Rails) [英] How to simulate database failure for test purposes (in Ruby on Rails)
问题描述
这是一个常见的设置,通过一些监视工具(例如Monit)通过心跳消息来保存应用程序。如果应用程序正在运行并且一切正常,则它返回一个I am alive消息,如果数据库失败或Web服务器挂起,则不返回任何内容或内部服务器错误(HTTP状态代码500)页面。您如何模拟数据库失败以在Ruby on Rails中测试此行为?如果可以在测试( Test :: Unit
或 RSpec
)本身。
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.
推荐答案
看起来像是可以使用 ActiveRecord :: Base.remove_connection
以模拟数据库故障。使用RSpec这将是:
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
这篇关于如何模拟数据库失败的测试目的(在Ruby on Rails)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!