# Unit tests don't work where model name is different from tab

**URL:** <https://www.ruby-forum.com/t/unit-tests-dont-work-where-model-name-is-different-from-tab/48668>\
**Category:** Rails\
**Created:** [November 23, 2005, 4:14am UTC](https://www.ruby-forum.com/t/unit-tests-dont-work-where-model-name-is-different-from-tab/48668 "2005-11-23T04:14:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtmusko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@mtmusko](https://www.ruby-forum.com/u/mtmusko)\
**Post date:** [November 23, 2005, 4:14am UTC](https://www.ruby-forum.com/t/unit-tests-dont-work-where-model-name-is-different-from-tab/48668/1 "2005-11-23T04:14:47Z")

</div>

I have a model called Group. It uses the table ‘covenant\_groups’.

When I ran a unit test it complained it couldn’t find the groups table.  
So  
I renamed the fixture file from “groups.yml” to “covenant\_groups.yml”  
and  
changed the fixtures line in my unit test to “fixtures  
:covenant\_groups”.

However now I can’t access anything from my fixture, it always complains  
it’s null. For example, covenant\_groups(:allstars) always returns null  
when  
it should return a Group object.

Does anyone know why this is happening? Is this a fixtures bug?

---

<div class="post-metadata">

**Author:** ![mtmusko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@mtmusko](https://www.ruby-forum.com/u/mtmusko)\
**Post date:** [November 23, 2005, 4:38am UTC](https://www.ruby-forum.com/t/unit-tests-dont-work-where-model-name-is-different-from-tab/48668/2 "2005-11-23T04:38:58Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Nov 22, 2005, at 7:11 PM, Frank K. wrote:

> Does anyone know why this is happening? Is this a fixtures bug?

Hmm. Is your covenant\_groups.yml valid YAML? Try  
$ ruby -ryaml -e “puts YAML.load\_file(‘test/fixtures/  
covenant\_groups.yml’).inspect”

Does your Group model work in ./script/console?  
$ ./script/console

> Group.find(:all)

jeremy  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDg+PWAQHALep9HFYRAs/xAKDEUVdRTyMFY/1Y8gMV13M62zP0PwCbBJGM  
1g2KCCoSGMygAFepp4qr2n0=  
=N/4f  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![mtmusko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@mtmusko](https://www.ruby-forum.com/u/mtmusko)\
**Post date:** [November 23, 2005, 5:23am UTC](https://www.ruby-forum.com/t/unit-tests-dont-work-where-model-name-is-different-from-tab/48668/3 "2005-11-23T05:23:13Z")

</div>

You can’t just overwrite table\_name in active record. You have to use  
set\_table\_name now. Why? I don’t know. This is something new. I did see  
that the example in the documentation shows the set\_table\_name method.

ModelClass \< ActiveRecord::Base  
set\_table\_name “different\_table”  
end

This will not work:  
ModelClass \< ActiveRecord::Base  
def table\_name  
“different\_table”  
end  
end

---

<div class="post-metadata">

**Author:** ![mtmusko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@mtmusko](https://www.ruby-forum.com/u/mtmusko)\
**Post date:** [November 23, 2005, 9:13pm UTC](https://www.ruby-forum.com/t/unit-tests-dont-work-where-model-name-is-different-from-tab/48668/4 "2005-11-23T21:13:10Z")

</div>

Hi Jeremy,

I am confident covenant\_groups.yml is valid because it writes into the  
database the expected values. My actual application also works  
correctly,  
it interacts correctly with the Group model which uses the  
covenant\_groups  
table.

I think this must be a fixtures bug. What do you think?

-Frank

---

<div class="post-metadata">

**Author:** ![mtmusko](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecccb3/32.png) [@mtmusko](https://www.ruby-forum.com/u/mtmusko)\
**Post date:** [November 23, 2005, 9:57pm UTC](https://www.ruby-forum.com/t/unit-tests-dont-work-where-model-name-is-different-from-tab/48668/5 "2005-11-23T21:57:36Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Nov 23, 2005, at 12:08 PM, Frank K. wrote:

> I am confident covenant\_groups.yml is valid because it writes into  
> the database the expected values. My actual application also works  
> correctly, it interacts correctly with the Group model which uses  
> the covenant\_groups table.
> 
> I think this must be a fixtures bug. What do you think?

Sorry; I wasn’t thinking. We can’t look up the class for a custom  
table name.

covenant\_groups(:some\_name) is trying to load an instance of the  
CovenantGroup class, which doesn’t exist.

This is expected, since you can’t specify the class for a fixtures  
table. It will change after 1.0.

In the meantime, try  
CovenantGroup = Group  
at the top of your test file.

Then the fixture will map covenant\_groups table to ConvenantGroup  
class, so covenant\_groups(:foo) should work. But, untested 🙂

jeremy  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDhNeFAQHALep9HFYRAi6rAJ4iw4QafT5zgrsmPUcD2EvHW4Ci2gCgxMzw  
SqqRs5n+E4RG/ldQ1Y2a2L4=  
=Z6Y7  
-----END PGP SIGNATURE-----
