Pat Maddox
2005-Dec-04 00:13 UTC
Fixtures not working in functional tests when run through Rake
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
Blair Zajac
2005-Dec-04 05:59 UTC
Re: Fixtures not working in functional tests when run through Rake
Pat Maddox wrote:> 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. > > PatPat, 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. Regards, Blair -- Blair Zajac, Ph.D. <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org> Subversion and Orca training and consulting http://www.orcaware.com/svn/
Pat Maddox
2005-Dec-04 06:07 UTC
Re: Fixtures not working in functional tests when run through Rake
On 12/3/05, Blair Zajac <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org> wrote:> Pat Maddox wrote: > > 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. > > Regards, > BlairYes 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: $ rake test_functional ... 1) Error: test_create_without_name(Controllers::Admin::CategoryControllerTest): ActiveRecord::RecordNotFound: Couldn''t find User with ID=1003 /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.rb:428:in `find'' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:390:in `find'' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:456:in `users'' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:452:in `users'' ./test/functional/admin/category_controller_test.rb:16:in `setup_without_fixtures'' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:522:in `setup'' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:520:in `setup'' 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. Pat
Pat Maddox
2005-Dec-04 06:08 UTC
Re: Fixtures not working in functional tests when run through Rake
On 12/3/05, Blair Zajac <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org> wrote:> Pat Maddox wrote: > > 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. > > Regards, > BlairI forgot to mention, I''ve been using your teardown method to clear the tables after each test.
Blair Zajac
2005-Dec-04 06:15 UTC
Re: Fixtures not working in functional tests when run through Rake
Pat Maddox wrote:> On 12/3/05, Blair Zajac <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org> wrote: > > 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?> $ rake test_functional > ... > 1) Error: > test_create_without_name(Controllers::Admin::CategoryControllerTest):What does test_create_without_name look like?> ActiveRecord::RecordNotFound: Couldn''t find User with ID=1003 > /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.rb:428:in > `find'' > /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:390:in > `find'' > /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:456:in > `users'' > /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:452:in > `users'' > ./test/functional/admin/category_controller_test.rb:16:in > `setup_without_fixtures'' > /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:522:in > `setup'' > /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:520:in > `setup'' > > 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)What does users(:firstuser) do? So if you type ruby test/functional/admin/category_controller_test.rb the test runs? Regards, Blair
Pat Maddox
2005-Dec-04 06:30 UTC
Re: Fixtures not working in functional tests when run through Rake
On 12/3/05, Blair Zajac <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org> wrote:> Pat Maddox wrote: > > On 12/3/05, Blair Zajac <blair-szbw9MROnEZWk0Htik3J/w@public.gmane.org> wrote: > > > > 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?Yes> > > $ rake test_functional > > ... > > 1) Error: > > test_create_without_name(Controllers::Admin::CategoryControllerTest): > > What does test_create_without_name look like?def test_create_without_name num_categories = Category.count post :create, :category => { } assert_response :success assert_equal num_categories, Category.count end> > ActiveRecord::RecordNotFound: Couldn''t find User with ID=1003 > > /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/base.rb:428:in > > `find'' > > /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:390:in > > `find'' > > /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:456:in > > `users'' > > /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:452:in > > `users'' > > ./test/functional/admin/category_controller_test.rb:16:in > > `setup_without_fixtures'' > > /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:522:in > > `setup'' > > /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active_record/fixtures.rb:520:in > > `setup'' > > > > 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) > > 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.> So if you type > > ruby test/functional/admin/category_controller_test.rb > > the test runs?Yes 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. Pat
Pat Maddox
2005-Dec-04 07:07 UTC
Re: Fixtures not working in functional tests when run through Rake
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. Pat
Pat Maddox
2005-Dec-04 07:33 UTC
Re: Fixtures not working in functional tests when run through Rake
On 12/4/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.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. Pat
Pat Maddox
2005-Dec-04 08:23 UTC
Re: Fixtures not working in functional tests when run through Rake
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
Pat Maddox
2005-Dec-04 08:44 UTC
Re: Fixtures not working in functional tests when run through Rake
On 12/4/05, Pat Maddox <pergesu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.