views:

271

answers:

1

I'm building a Rails application that requires some models to interact with some necessary Java libraries. To do this, I want to use Rails on on MRI - not JRuby or RBJ. The Java code only needs read access to the database tables managed by the Rails app.

One way to do this is to set up a Servlet that reads the Rails app's database tables.

The problem is that during testing, using Rails transactional fixtures makes all database activity during the test invisible to external processes such as the Servlet.

For deployment, this isn't a problem, but it's not real clear how to do automated testing under these conditions.

How can I use RSpec to test Rails models that depend on external processes having access to the tables managed by Rails?

+2  A: 

Turn off transactional fixtures and the database writes should become visible to external processes. In the test class that you want to do this with put:

self.use_transactional_fixtures = false

Or put it in test/test_helper.rb if you want it to apply to everything. Note that your tests may run slower because everything's actually hitting the database.

wulong
Thanks. The downside would seem to be that the database no longer gets cleared before each test is run. I could clear it manually in a before(:each).I'd really like something that's a little more automated. Are there any other ways around this problem?
Rich Apodaca
If you're relying on the data to be visible externally, then, no there's no other way. You'll have to clear and reinstantiate your fixtures after every run. You can do this without writing extra code by adding `fixtures :model1, :model2, ...` to your test class.
wulong