views:

1240

answers:

1

What is the best way to test a model that is using a different database connection in Rails. For example I have a model FooBar that is read only:

class FooBar < ActiveRecord::Base
  establish_connection configurations['foo_bars']
  # ...
end

Are there any good conventions, hacks, or plugins out there?

+1  A: 

In my experience, once the connection is established you can treat the model just like any other model. Since you are just consuming the data that will simplify some of the testing as you wont need to test for data validations.

Obviously because Rails is talking to two different databases from two different models, you won't be able to do joins between the databases and so there will be nothing to test there either.

So, to answer the question: what is the best way to test a model that is using a second database? I would say, exactly the same way that you would test it if it was your only database.

I find that in my models that wrap my legacy databases I have to add some special tie in code that makes things a little more "Rails-y" and makes the view and controller code look like there are has_one and belongs_to type code in there. I do have tests that exercise those custom methods.

Hope that helps.

salt.racer