# Testing STI models

**URL:** <https://www.ruby-forum.com/t/testing-sti-models/88342>\
**Category:** Rails\
**Created:** [March 28, 2006, 1:08pm UTC](https://www.ruby-forum.com/t/testing-sti-models/88342 "2006-03-28T13:08:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![xamdam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/d78d45/32.png) [@xamdam](https://www.ruby-forum.com/u/xamdam)\
**Post date:** [March 28, 2006, 1:08pm UTC](https://www.ruby-forum.com/t/testing-sti-models/88342/1 "2006-03-28T13:08:33Z")

</div>

Hi everyone!

I have a question: what is the preferred way (in terms of simplicity) to  
test models with single table inheritance. Right now I have 1 test case  
and 1 fixture for all of the STI models, but it’s very hard to maintain  
it (too many tests and records in fixture). It would be better if  
fixtures could be named separately from DB tables.

I know, Engines plugin has it’s own “fixture” helper that allows class  
and table name to be specified for particular fixture. Will it sometime  
become a standard (and be included in Rails) or there are other ways /  
thoughts on this ?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![xamdam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/d78d45/32.png) [@xamdam](https://www.ruby-forum.com/u/xamdam)\
**Post date:** [March 28, 2006, 1:46pm UTC](https://www.ruby-forum.com/t/testing-sti-models/88342/2 "2006-03-28T13:46:03Z")

</div>

There is a ticket (#1911 as I recall) which holds essentially the same  
code as the engines plugin uses - your best bet would be to:

a) petition the core team so they know that this functionality is  
considered valuable by many people  
b) (more importantly) ensure the patch is still valid against trunk

- james

On 3/28/06, Maxim K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> become a standard (and be included in Rails) or there are other ways /

–

- J \*  
~

---

<div class="post-metadata">

**Author:** ![xamdam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/x/d78d45/32.png) [@xamdam](https://www.ruby-forum.com/u/xamdam)\
**Post date:** [March 28, 2006, 5:56pm UTC](https://www.ruby-forum.com/t/testing-sti-models/88342/3 "2006-03-28T17:56:20Z")

</div>

On 3/28/06, Maxim K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> become a standard (and be included in Rails) or there are other ways /  
> thoughts on this ?
> 
> Thanks in advance

The core team isn’t particularly enthusiastic about this feature. I  
did a patch against trunk for it the other day, and various people  
said it worked fine, but wasn’t necessary.

In the meantime, you can use the new subdirectory support for fixtures  
to make your life easier.  
Assuming your superclass model is called ‘Example’, and your table  
name is ‘examples’:

Create test/unit/fixtures/examples.yml. You can put fixtures in here,  
or just leave it blank. It has to exist, or the current fixture code  
won’t load the subdirectories.

Create a new directory: test/unit/fixtures/examples/  
Inside that directory, you can create as many .yml files as you want,  
with any names you desire, and they will be picked up as fixtures for  
‘example’.

In your unit tests, call “fixtures :examples”  
All of the STI subclass fixtures will be in one big pile (e.g.  
examples(:some\_fixture\_name) ), but at least you can keep your test  
data in separate files. If you name the fixtures well, it isn’t too  
confusing.

Personally, I much prefer being able to access my fixtures by the name  
of the model, rather than of the table… but that’s off the list for  
now.

–Wilson.
