Forum: RSpec How to dummy a DB adapter?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
171ea139761951336b844e708d1547ab?d=identicon&s=25 James Byrne (byrnejb)
on 2009-03-11 19:02
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.
3880f04333bf8375b44cd6aa135406c7?d=identicon&s=25 Bart Zonneveld (Guest)
on 2009-03-12 09:46
(Received via mailing list)
On 11-mrt-2009, at 19:02, James Byrne 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
This topic is locked and can not be replied to.