# Mocking or stubbing an external database

**URL:** <https://www.ruby-forum.com/t/mocking-or-stubbing-an-external-database/237320>\
**Category:** RSpec\
**Created:** [November 6, 2013, 6:23am UTC](https://www.ruby-forum.com/t/mocking-or-stubbing-an-external-database/237320 "2013-11-06T06:23:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [November 6, 2013, 6:23am UTC](https://www.ruby-forum.com/t/mocking-or-stubbing-an-external-database/237320/1 "2013-11-06T06:23:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gema](https://www.ruby-forum.com/user_avatar/www.ruby-forum.com/gema/32/442_2.png) [@gema](https://www.ruby-forum.com/u/gema)\
**Post date:** [October 18, 2018, 6:07pm UTC](https://www.ruby-forum.com/t/mocking-or-stubbing-an-external-database/237320/2 "2018-10-18T18:07:24Z")

</div>

> [@musicdenotation](#):
>
> Rails establi

try to wrap your database as a repository, agnostic to the internal structure of the database
