Forum: RSpec mocking or stubbing an external database

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Fearless F. (Guest)
on 2013-11-06 07:23
I'm working on a system that requires me to attach to an external
(readonly) database.  In order to prevent Rails establishing a
connection to the external database for every test, I've written
something like this:

  module External
    class ExternalDBModel < ActiveRecord::Base
      establish_connection(:external_db) unless Rails.env == "test"
      def readonly? ; return true ; end

... and all model that are backed by the external database inherit from
ExternalDBModel.  This works well: my RSpec tests run fast and don't
require a network connection.


I have a locally backed ProxyHome model that interacts with an
externally backed External::Home model through a belongs_to / has_one

  class ProxyHome < ActiveRecord::Base
    belongs_to :home, :class_name "External::Home", :foreign_key

  module External
    class Home < ExternalDBModel
      has_one :proxy_home, :class_name "ProxyHome", :foreign_key

Here's my problem: I want to do various RSpec tests on the ProxyHome
model, but I haven't been able to stub around the reference the
ExternalHome without getting an error.  For example:

    it 'prefers external name' do
      home = mock_model("External::Home", :name => "George Burns")
      proxy_home = => home, :owner_name => "Ordinary
      proxy_home.owner_name.should == "George Burns"

dies with a message (even though I've passed "External::Home" as a

  1) ProxyHome prefers external name
     Failure/Error: home = mock_model("External::Home", :name => "George
       wrong constant name External::Home
     # ./spec/models/proxy_home_spec.rb:56:in `block (3 levels) in <top

Attempting to stub_mode(External::Home) dies because there is no local
table named "homes".

I suppose I could set the table name of External::Home to a dummy local
table when I'm in testing mode, but that feels awkward.  What's the
right approach to testing this kind of thing?

-- ff

Ruby 2.0.0
Rails 4.0.0
rspec (2.14.1)
rspec-core (2.14.5)
rspec-expectations (2.14.3)
rspec-mocks (2.14.3)
rspec-rails (2.14.0)
OS X 10.8.5
This topic is locked and can not be replied to.