I’ve got some functional tests that load up fixtures…and they run
just fine when I run them through the Ruby interpretter. But if I run
them with Rake, it says it can’t find any of the records and causes a
bunch of errrors. No idea why this is happening…does Rake do stuff
differently than just running the individual Ruby file? Thanks for
any help.
I’ve got some functional tests that load up fixtures…and they run
just fine when I run them through the Ruby interpretter. But if I run
them with Rake, it says it can’t find any of the records and causes a
bunch of errrors. No idea why this is happening…does Rake do stuff
differently than just running the individual Ruby file? Thanks for
any help.
Pat
Pat,
Can you send the first several errors you’re getting, along with the
command
that Rake is running?
You can try running the same command that rake does from the command
line. I
typically do that, but only list several test *.rb files at once to see
what’s
going on.
The other thing is to tail the log/test.log.
Are you using foreign keys in your schema? I am and when I ran into
these
problems, I didn’t have the fixtures defined correctly for each of my
models or
controllers and didn’t clean up the fixtures. I ended up putting this
into my
test/test_helper.rb to clean up fixtures:
Delete the fixtures associated with this test suite in the reverse
order that they are loaded. Take care to use the database
connection associated with the fixture, not with the model being
tested.
def teardown
self.class.fixture_table_names.reverse.each do |table_name|
klass_name = Inflector.classify(table_name.to_s)
if Object.const_defined?(klass_name)
klass = Object.const_get(klass_name)
klass.connection.delete(“DELETE FROM #{table_name}”, ‘Fixture
Delete’)
else
flunk(“Cannot find class for table ‘#{table_name}’ to delete
fixtures”)
end
end
end
Rake will run either the clone_structure_to_test or the
clone_schema_to_test
task before running the test.
Pat,
Are you using foreign keys in your schema? I am and when I ran into these
klass_name = Inflector.classify(table_name.to_s)
task before running the test.
Regards,
Blair
I forgot to mention, I’ve been using your teardown method to clear the
tables after each test.
Yes I’m using foreign keys, but all the unit tests run just fine.
It’s only the functional tests that have a problem with the fixtures
apparently, and only when I run rake:
Just to be dumb about this, but you have the ‘fixture’ lines in the
functional
tests to load the data?
If you load script/console and try to find the user id 1003, do you find
it?
Pat,
Are you using foreign keys in your schema? I am and when I ran into these
klass_name = Inflector.classify(table_name.to_s)
task before running the test.
Regards,
Blair
Yes I’m using foreign keys, but all the unit tests run just fine.
It’s only the functional tests that have a problem with the fixtures
apparently, and only when I run rake:
That’s repeated for every test I run. In the test, I’m trying to open
the fixture in the setup method:
def setup @controller = TicketController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new
# Log the user in
@request.session[:user] = users(:firstuser)
end
Line 16 is the @request.session… line, the error referencing
setup_without_fixtures.
Yes I’m using foreign keys, but all the unit tests run just fine.
It’s only the functional tests that have a problem with the fixtures
apparently, and only when I run rake:
Just to be dumb about this, but you have the ‘fixture’ lines in the functional
tests to load the data?
fixtures :categories, :users, :tickets, :messages
If you load script/console and try to find the user id 1003, do you find it?
# Log the user in
@request.session[:user] = users(:firstuser)
What does users(:firstuser) do?
I commented out all the tests and just did a test_truth method, and
puts users(:firstuser).login in the setup method. It output the login
fine. So now I’m really confused as to what’s wrong.
Another thing that’s confusing me is that if I select * from users
after I run the functional test, I get all the records that are
defined in the fixtures file. I don’t think I should though, because
they should all be deleted in the teardown method, right? I know the
teardown method is being called because I’ve put some puts statements
in there to make sure.
if self.use_transactional_fixtures?
ActiveRecord::Base.connection.execute("COMMIT")
end
Before and after (not at the same time…individual trials) the main
loop in the teardown method. Still getting errors for the tests.
Okay, turns out it should be a rollback, not a commit. I changed the
teardown method to
Add more helper methods to be used by all tests here…
def teardown
self.class.fixture_table_names.reverse.each do |table_name|
klass_name = Inflector.classify(table_name.to_s)
if Object.const_defined?(klass_name)
klass = Object.const_get(klass_name)
klass.connection.delete(“DELETE FROM #{table_name}”, ‘Fixture
Delete’)
else
flunk(“Cannot find class for table ‘#{table_name}’ to delete
fixtures”)
end
end
if self.use_transactional_fixtures?
ActiveRecord::Base.connection.execute(“ROLLBACK”)
end
end
I guess rake isn’t doing the rollback in the functional tests? Not
sure why really. The rollback is made when I do “ruby
test/functional/test.rb” but not when I run it through rake. This
means that the rollback is executed twice when I just run the test
through ruby…but so far it doesn’t seem to be complaining.
Not sure if this is a bug in the rake tasks, or if I’m just
misunderstanding what’s supposed to happen. Hopefully someone who
knows more can shed some more light.
Fun and interesting new development. If I set
use_transactional_fixtures to false, all my tests run fine using rake.
They run pretty slowly, but at least they’re running now. I got the
idea to do this after checking the test log file. With transactional
fixtures, I see:
SQL (0.001503) BEGIN
User Load (0.008182) SELECT * FROM users WHERE (users.id = 1001)
LIMIT 1
Fixture Delete (0.001562) DELETE FROM messages
Fixture Delete (0.005275) DELETE FROM tickets
Fixture Delete (0.001460) DELETE FROM users
Fixture Delete (0.001313) DELETE FROM categories
SQL (0.001495) BEGIN
User Load (0.012900) SELECT * FROM users WHERE (users.id = 1001)
LIMIT 1
Fixture Delete (0.001634) DELETE FROM messages
Fixture Delete (0.001371) DELETE FROM tickets
Fixture Delete (0.001534) DELETE FROM users
Fixture Delete (0.001459) DELETE FROM categories
Something about that doesn’t look right. Maybe it’s that the custom
teardown isn’t committing the transaction? I’ve tried throwing a
if self.use_transactional_fixtures?
ActiveRecord::Base.connection.execute("COMMIT")
end
Before and after (not at the same time…individual trials) the main
loop in the teardown method. Still getting errors for the tests.
Just so I can continue the trend of responding to my own thread…I
was trying to reproduce the error and figure out exactly what the
error messages were. I commented out the teardown method, ran rake,
and got no errors. Now I’m just thoroughly confused. I’ve got
absolutely none of the code or solutions that are described at http://dev.rubyonrails.org/ticket/2404 yet I’m getting no errors. sigh Guess I’ll just spend lots of time with svn diff to see what’s
changed.
Go figure, they just decided to randomly stop working all of a sudden.
I have absolutely no clue how to diagnose any of this. Giving up on
it for the night…hopefully someone has an idea.
Just so I can continue the trend of responding to my own thread…I
was trying to reproduce the error and figure out exactly what the
error messages were. I commented out the teardown method, ran rake,
and got no errors. Now I’m just thoroughly confused. I’ve got
absolutely none of the code or solutions that are described at http://dev.rubyonrails.org/ticket/2404 yet I’m getting no errors. sigh Guess I’ll just spend lots of time with svn diff to see what’s
changed.
Pat
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.