Fixtures, testing and namespaces

Hi Folks,

I am sure that someone, somewhere, has encountered this, but I’ve not
been able to find the answers yet.

I have some name-spaced models. My application relies on another
database over which I have limited access (let’s call it the “Main”
database) and therefore I have a models in the app/models/main
directory that are name-spaced like so: Main::User and Main::Item,
etc… There is an abstract base class from which these name-spaced
models derive that establishes the connection to the main database.

All good so far.

As I enrichen the series of test for my application (using RSpec, but
I do not believe that is the issue), I have run into trouble trying to
create parallel structures in my spec directory in which I can place
fixtures.

My goal is to have rich and suitable data in my test database to test
my models, views and controllers. To do that, I also need a rich and
suitable data that mimics the Main database. Obviously, I cannot
simply create and drop tables on this Main database. So I have a
migration to build some of the tables from the Main database in my
database for testing purposes. A simple statement in the abstract
base class establishes the correct connection based upon the value of
the RAILS_ENV variable. So, during test, the Main models point to my
local test database, which contains my application data and tables
from the Main database.

I have created some fixtures to pump reliable data into the Main
database tables during testing. At first, I put them in a sub-
directory in the spec/fixtures directory. That is; spec/fixtures/main/
users.yml for example. However, I cannot specify a name-spaced
fixture in my test file. I’ve tried statements such as:

fixtures :main/users
fixtures :‘main/users’
fixtures ‘main/users’.to_sym

et cetera. Desperate efforts, I know. :slight_smile:

So I removed the name-spacing of the fixtures and moved the YML files
up one directory. However, now the corresponding class for the
fixture (main/user) cannot be found because the testing infrastructure
is looking for “User” and not “Main::User”

Has anyone figured out this little puzzle?

Many thanks,

Peter

fixtures :main_users

That should work with your namespaced models. Then you need to specify
the fixtures class (do so beneath the above fixtures method call).

set_fixture_class :main_users => Main::User

Fixtures file main_users.yml can go in the test/fixtures directory.