# Test data life cycle confusion

**URL:** <https://www.ruby-forum.com/t/test-data-life-cycle-confusion/100029>\
**Category:** Rails\
**Created:** [February 1, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/test-data-life-cycle-confusion/100029 "2006-02-01T23:52:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![evronm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f1d935/32.png) [@evronm](https://www.ruby-forum.com/u/evronm)\
**Post date:** [February 1, 2006, 11:52pm UTC](https://www.ruby-forum.com/t/test-data-life-cycle-confusion/100029/1 "2006-02-01T23:52:39Z")

</div>

Hi,

It states on page 140 of Agile Web D. with Rails:

Here?s the bottom line: even if a test method updates the test database,  
the database is put back to its default state before the next test  
method is  
run. This is important because we don?t want tests to become dependent  
on the results of previous tests.

Well, I’ve been running into a brick wall with some functional tests  
based off of what the scaffold generator created for me.

Here’s a couple snippets:  
articles.yml:  
first:  
id: 1  
published: 1  
publish\_on: 2006-01-24 14:50:30

articles\_controller\_test.rb:  
  
def test\_edit  
get :edit, :id =\> 1, :version\_id =\> 1

assert\_response :success  
assert\_template ‘edit’

assert\_not\_nil assigns(:article)  
assert assigns(:article).valid?  
end  
def test\_destroy  
assert\_not\_nil Article.find(1)

post :destroy, :id =\> 1  
assert\_response :redirect  
assert\_redirected\_to :action =\> ‘list’

assert\_raise(ActiveRecord::RecordNotFound) {  
Article.find(1)  
}  
end

And finally, the error that’s being thrown when this is run:

1. Error:  
test\_edit(Controllers::Admin::ArticlesControllerTest):  
ActiveRecord::RecordNotFound: Couldn’t find Article with ID=1

So, it looks like test\_destroy is being run first… any idea why my  
fixtures aren’t being reloaded before each test is being run? Is there  
something else I should be doing?

Thanks in advance,  
Mike

---

<div class="post-metadata">

**Author:** ![evronm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f1d935/32.png) [@evronm](https://www.ruby-forum.com/u/evronm)\
**Post date:** [February 2, 2006, 12:20am UTC](https://www.ruby-forum.com/t/test-data-life-cycle-confusion/100029/2 "2006-02-02T00:20:22Z")

</div>

Mike E. wrote:

> So, it looks like test\_destroy is being run first…  
> Yup… They’re run in alphabetical order.

> any idea why my  
> fixtures aren’t being reloaded before each test is being run? Is there  
> something else I should be doing?  
> Silly question, but let’s get it out of the way: Do you have a  
> fixtures :articles  
> line in your testcase? If it’s come direct from the scaffold, it should  
> be there, but just in case…

If they weren’t loaded at all, that would have the same effect.

Oh, and what’s your database and version?

---

<div class="post-metadata">

**Author:** ![evronm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f1d935/32.png) [@evronm](https://www.ruby-forum.com/u/evronm)\
**Post date:** [February 2, 2006, 12:38am UTC](https://www.ruby-forum.com/t/test-data-life-cycle-confusion/100029/3 "2006-02-02T00:38:42Z")

</div>

Alex Y. wrote:

> Silly question, but let’s get it out of the way: Do you have a  
> fixtures :articles  
> line in your testcase? If it’s come direct from the scaffold, it  
> should be there, but just in case…

Yep,  
fixtures :articles, :article\_versions  
to be exact. Odd enough, when I run the test\_edit directly:  
$ ruby articles\_controller\_test.rb -n test\_edit  
it runs fine.

> If they weren’t loaded at all, that would have the same effect.
> 
> Oh, and what’s your database and version?

MySQL 4.1.16 and I’m using the mysql gem.

Another problem to note is when writing my tests my fixtures aren’t  
available directly in my test cases:  
first:  
id: 1  
published: 1  
publish\_on: 2006-01-24 14:50:30

And if I try to use @first in the test, it comes back as nil. Could the  
problems be related?

Thanks,  
Mike

---

<div class="post-metadata">

**Author:** ![evronm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f1d935/32.png) [@evronm](https://www.ruby-forum.com/u/evronm)\
**Post date:** [February 2, 2006, 6:46pm UTC](https://www.ruby-forum.com/t/test-data-life-cycle-confusion/100029/4 "2006-02-02T18:46:49Z")

</div>

Mike,

I recommend [http://www.clarkware.com/cgi/blosxom/2005/10/24](http://www.clarkware.com/cgi/blosxom/2005/10/24)

The cause of your problem is probably that you’re using transactional  
fixtures and your MySQL tables are MyISAM not InnoDB. For a  
quick/dirty fix set

use\_transactional\_fixtures = false

in your test\_helper.rb

> And if I try to use @first in the test, it comes back as nil. Could the  
> problems be related?

If you want to be able to use @first you must have

self.use\_instantiated\_fixtures = true

in your test\_helper.rb. Otherwise, use articles(:first) instead.

cheers  
Gerret
