Mocking or stubbing an external database


#1

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
end
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.

Except…

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

class ProxyHome < ActiveRecord::Base
belongs_to :home, :class_name “External::Home”, :foreign_key
“home_id”

end

module External
class Home < ExternalDBModel
has_one :proxy_home, :class_name “ProxyHome”, :foreign_key
“home_id”

end
end

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 = ProxyHome.new(:home => home, :owner_name => "Ordinary

Guy")
proxy_home.owner_name.should == “George Burns”
end

dies with a message (even though I’ve passed “External::Home” as a
string):

  1. ProxyHome prefers external name
    Failure/Error: home = mock_model(“External::Home”, :name => “George
    Burns”)
    NameError:
    wrong constant name External::Home

    ./spec/models/proxy_home_spec.rb:56:in `block (3 levels) in <top

(required)>’

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

P.S.:
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