How to dummy a DB adapter?


#1

I am writing a library module to handle certain StatementInvalid type AR
exceptions. I wish to catch the case where the db adapter in use is not
supported by the module. I would like to test this situation and have
only a vague idea as to how to proceed.

My exception handling method selects the parsing method based on the
adapter_name value.

def hll_ar_exception(exception)
raise exception unless exception.class.to_s ==
‘ActiveRecord::StatementInvalid’
acan = self.connection.adapter_name
case acan
when ‘MySQL’
hll_parse_mysql
when ‘PostgreSQL’
hll_parse_postgresql
when ‘SQLite’
hll_parse_sqlite(exception)
else
Rails::logger.warn(“Adapter #{acan} not supported by
hll_ar_exception.”)
raise exception
end
end

I am thinking about simply opening SQLiteAdapter and overriding the
adapter_name method to some arbitrary value for the test. However, it
occurs to me that there are probably better ways to do what I am trying
to accomplish so I welcome any suggestions on how to proceed.


#2

On 11-mrt-2009, at 19:02, James B. wrote:

I am thinking about simply opening SQLiteAdapter and overriding the
adapter_name method to some arbitrary value for the test. However, it
occurs to me that there are probably better ways to do what I am
trying
to accomplish so I welcome any suggestions on how to proceed.

http://rspec.info/documentation/mocks/stubs.html

Stubbing is your newfound friend.

gr,
bartz