# Defining fixtures in Unit Tests necessary?

**URL:** <https://www.ruby-forum.com/t/defining-fixtures-in-unit-tests-necessary/163907>\
**Category:** Rails\
**Created:** [March 26, 2009, 1:28am UTC](https://www.ruby-forum.com/t/defining-fixtures-in-unit-tests-necessary/163907 "2009-03-26T01:28:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yachtman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d07c76/32.png) [@yachtman](https://www.ruby-forum.com/u/yachtman)\
**Post date:** [March 26, 2009, 1:28am UTC](https://www.ruby-forum.com/t/defining-fixtures-in-unit-tests-necessary/163907/1 "2009-03-26T01:28:30Z")

</div>

Just sinking my teeth into tests and the going is tough.

Is it necessary to define fixtures in my test file  
(‘fixtures :users’)?

My test are working with the definition, but I decided to debug and  
checked for the fixtures existence even without the definition and all  
of them had been loaded. So what’s the purpose of the declaration?

---

<div class="post-metadata">

**Author:** ![yachtman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d07c76/32.png) [@yachtman](https://www.ruby-forum.com/u/yachtman)\
**Post date:** [March 26, 2009, 1:41pm UTC](https://www.ruby-forum.com/t/defining-fixtures-in-unit-tests-necessary/163907/2 "2009-03-26T13:41:07Z")

</div>

Phlip,

Well said. Hadn’t thought of that possibility, that by declaring the  
fixtures, it would reload them. Smart and makes sense.

Thanks!!

---

<div class="post-metadata">

**Author:** ![yachtman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d07c76/32.png) [@yachtman](https://www.ruby-forum.com/u/yachtman)\
**Post date:** [March 26, 2009, 3:09am UTC](https://www.ruby-forum.com/t/defining-fixtures-in-unit-tests-necessary/163907/3 "2009-03-26T03:09:10Z")

</div>

yachtman wrote:

> Just sinking my teeth into tests and the going is tough.

Not as tough as debugging all night before a release. Stick with TDD and  
you  
will never look back!

> Is it necessary to define fixtures in my test file  
> (‘fixtures :users’)?
> 
> My test are working with the definition, but I decided to debug and  
> checked for the fixtures existence even without the definition and all  
> of them had been loaded. So what’s the purpose of the declaration?

Of course they are there - you loaded them. However, consider this test  
suite:

class SomeTests …

```
def test_aardvark
  screw_users_table_up()
  assert{ users_table_is_screwed_up() }
end

def test_zebra
  deny{ users_table_is_screwed_up() }
end

```

end

The test case names force [the current version of] test/unit to run  
those cases  
in that order.

The second test will fail because the first one screwed the table up.

The ‘fixtures :users’ line means “please un-screw-up the users table  
between  
each test case”. Adding it to my hypothetical test suite would “fix” it.

The fixture system enables “Test Isolation”. You could run any test  
case, in any  
order, or not at all, and each one should not add any noise to the  
signal from  
any other.

–  
Phlip

---

<div class="post-metadata">

**Author:** ![yachtman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d07c76/32.png) [@yachtman](https://www.ruby-forum.com/u/yachtman)\
**Post date:** [March 26, 2009, 8:25pm UTC](https://www.ruby-forum.com/t/defining-fixtures-in-unit-tests-necessary/163907/4 "2009-03-26T20:25:09Z")

</div>

On Thu, Mar 26, 2009 at 8:43 AM, Frederick C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> For a while rails apps (by default) have fixtures :all in their  
> test\_helper.rb, which does pretty much what you would imagine.

When I moved an app from 2.2.2 to 2.3.2 a couple days ago, I was  
prompted to remove the fixtures :all from my test\_helper.rb,  
deprecation warning if I recall correctly. And then removing it broke  
all my tests. I had to put explicit fixture entries back into in all  
my tests to get them working again.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<div class="post-metadata">

**Author:** ![yachtman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/d07c76/32.png) [@yachtman](https://www.ruby-forum.com/u/yachtman)\
**Post date:** [March 26, 2009, 2:44pm UTC](https://www.ruby-forum.com/t/defining-fixtures-in-unit-tests-necessary/163907/5 "2009-03-26T14:44:40Z")

</div>

On Mar 26, 12:27 am, yachtman [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is it necessary to define fixtures in my test file  
> (‘fixtures :users’)?
> 
> My test are working with the definition, but I decided to debug and  
> checked for the fixtures existence even without the definition and all  
> of them had been loaded. So what’s the purpose of the declaration?

For a while rails apps (by default) have fixtures :all in their  
test\_helper.rb, which does pretty much what you would imagine.

Fred
