Forum: Ruby on Rails Testing STI models

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8d6211d53a4f97822b08c0f7b63c3c35?d=identicon&s=25 Maxim Kulkin (maximkulkin)
on 2006-03-28 13:08
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
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 James Adam (Guest)
on 2006-03-28 13:46
(Received via mailing list)
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 Kulkin <maxim.kulkin@gmail.com> wrote:
> become a standard (and be included in Rails) or there are other ways /
>
--
* J *
  ~
25e11a00a89683f7e01e425a1a6e305c?d=identicon&s=25 Wilson Bilkovich (Guest)
on 2006-03-28 17:56
(Received via mailing list)
On 3/28/06, Maxim Kulkin <maxim.kulkin@gmail.com> 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 topic is locked and can not be replied to.