【发布时间】:2010-03-09 09:06:39
【问题描述】:
我为 Rails MySQLAdapter 编写了一个小猴子补丁,并希望将其打包以在我的其他项目中使用。我正在尝试为它编写一些测试,但我仍然是测试新手,我不知道如何测试它。有人可以帮我入门吗?
这是我要测试的代码:
unless RAILS_ENV == 'production'
module ActiveRecord
module ConnectionAdapters
class MysqlAdapter < AbstractAdapter
def select_with_explain(sql, name = nil)
explanation = execute_with_disable_logging('EXPLAIN ' + sql)
e = explanation.all_hashes.first
exp = e.collect{|k,v| " | #{k}: #{v} "}.join
log(exp, 'Explain')
select_without_explain(sql, name)
end
def execute_with_disable_logging(sql, name = nil) #:nodoc:
#Run a query without logging
@connection.query(sql)
rescue ActiveRecord::StatementInvalid => exception
if exception.message.split(":").first =~ /Packets out of order/
raise ActiveRecord::StatementInvalid, "'Packets out of order' error was received from the database. Please update your mysql bindings (gem install mysql) and read http://dev.mysql.com/doc/mysql/en/password-hashing.html for more information. If you're on Windows, use the Instant Rails installer to get the updated mysql bindings."
else
raise
end
end
alias_method_chain :select, :explain
end
end
end
end
谢谢。
【问题讨论】:
标签: ruby-on-rails unit-testing ruby-on-rails-plugins