Testing STI models

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

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. [email protected] wrote:

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

  • J *
    ~

On 3/28/06, Maxim K. [email protected] 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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs