# Fixtures, testing and namespaces

**URL:** <https://www.ruby-forum.com/t/fixtures-testing-and-namespaces/150913>\
**Category:** Rails\
**Created:** [October 14, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/fixtures-testing-and-namespaces/150913 "2008-10-14T22:08:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![editkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/977dab/32.png) [@editkid](https://www.ruby-forum.com/u/editkid)\
**Post date:** [October 14, 2008, 10:08pm UTC](https://www.ruby-forum.com/t/fixtures-testing-and-namespaces/150913/1 "2008-10-14T22:08:17Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![editkid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/977dab/32.png) [@editkid](https://www.ruby-forum.com/u/editkid)\
**Post date:** [October 16, 2008, 6:55am UTC](https://www.ruby-forum.com/t/fixtures-testing-and-namespaces/150913/2 "2008-10-16T06:55:40Z")

</div>

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.
