Test data life cycle confusion


#1

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


#2

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?


#3

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


#4

Mike,

I recommend 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