Hi, I''m having a problem setting up a fixture for a has_and_belongs_to_many relationship. Here is what I have done: - I create a join table (in this case people_projects) - I add the ''has_and_belongs_to'' to both of the classes - I create a people_projects.yaml file - I require fixtures people, project and people_project Everything runs fine until I put in the people_project fixture; then it complains about not being able to find a PersonProject constant. I assume it is looking for the class so that it can instantiate the objects to make them available to the test class, which isn''t going to work. Any suggestions how I can work round this? Thanks, R.
Rob Lally wrote:> I''m having a problem setting up a fixture for a has_and_belongs_to_many > relationship. > > - I create a join table (in this case people_projects) > - I add the ''has_and_belongs_to'' to both of the classes > - I create a people_projects.yaml file > - I require fixtures people, project and people_project > > Everything runs fine until I put in the people_project fixture; then it > complains about not being able to find a PersonProject constant. I > assume it is looking for the class so that it can instantiate the > objects to make them available to the test class, which isn''t going to > work. > > Any suggestions how I can work round this?Fixture load attempts to create convenient instance variables for each fixture loaded. If a fixture doesn''t have a corresponding model class, it fails. I never use these instance variables, so I had commented out the line that creates them. Here''s a patch that keeps the variables but only loads them if the corresponding model class exists: http://dev.rubyonrails.com/ticket/572 Calling find on each fixture for every test case really puts the molasses on your test suite. It''d be nice to be able to turn this feature off entirely -- who''s using it, in any case? On a test-speed tangent, here''s a nice hack for your Rakefile. It checks your files'' mtimes and only runs tests on anything that''s changed in the last 10 minutes. Make it the default task to encourage frequent, speedy testing. Best, jeremy # Look up tests for recently modified sources. def recent_tests(source_pattern, test_path, touched_since = 10.minutes.ago) FileList[source_pattern].map do |path| if File.mtime(path) > touched_since test = "#{test_path}/#{File.basename(path, ''.rb'')}_test.rb" test if File.exists?(test) end end.compact end desc ''Test recent changes.'' Rake::TestTask.new(''test_recent'') do |t| since = 10.minutes.ago touched = FileList[''test/**/*_test.rb''].select { |path| File.mtime(path) > since } + recent_tests(''app/models/*.rb'', ''test/unit'', since) + recent_tests(''app/controllers/*.rb'', ''test/functional'', since) if touched.empty? mins = ((Time.now - since).to_f / 60).to_i raise SystemExit, "Nothing changed in the last #{mins} minutes. Get to work!" end t.libs << ''test'' t.verbose = true t.test_files = touched.uniq end task :test_recent => [ :clone_development_structure_to_test ]
Thanks for your help, that works perfectly. R. Jeremy Kemper wrote:> Rob Lally wrote: > >>I''m having a problem setting up a fixture for a has_and_belongs_to_many >>relationship. >> >>- I create a join table (in this case people_projects) >>- I add the ''has_and_belongs_to'' to both of the classes >>- I create a people_projects.yaml file >>- I require fixtures people, project and people_project >> >>Everything runs fine until I put in the people_project fixture; then it >>complains about not being able to find a PersonProject constant. I >>assume it is looking for the class so that it can instantiate the >>objects to make them available to the test class, which isn''t going to >>work. >> >>Any suggestions how I can work round this? > > > Fixture load attempts to create convenient instance variables for each > fixture loaded. If a fixture doesn''t have a corresponding model class, > it fails. I never use these instance variables, so I had commented out > the line that creates them. Here''s a patch that keeps the variables but > only loads them if the corresponding model class exists: > > http://dev.rubyonrails.com/ticket/572 > > Calling find on each fixture for every test case really puts the > molasses on your test suite. It''d be nice to be able to turn this > feature off entirely -- who''s using it, in any case? > > On a test-speed tangent, here''s a nice hack for your Rakefile. It > checks your files'' mtimes and only runs tests on anything that''s changed > in the last 10 minutes. Make it the default task to encourage frequent, > speedy testing. > > Best, > jeremy > > > # Look up tests for recently modified sources. > def recent_tests(source_pattern, test_path, touched_since = 10.minutes.ago) > FileList[source_pattern].map do |path| > if File.mtime(path) > touched_since > test = "#{test_path}/#{File.basename(path, ''.rb'')}_test.rb" > test if File.exists?(test) > end > end.compact > end > > desc ''Test recent changes.'' > Rake::TestTask.new(''test_recent'') do |t| > since = 10.minutes.ago > touched = FileList[''test/**/*_test.rb''].select { |path| > File.mtime(path) > since } + > recent_tests(''app/models/*.rb'', ''test/unit'', since) + > recent_tests(''app/controllers/*.rb'', ''test/functional'', since) > > if touched.empty? > mins = ((Time.now - since).to_f / 60).to_i > raise SystemExit, "Nothing changed in the last #{mins} minutes. Get > to work!" > end > > t.libs << ''test'' > t.verbose = true > t.test_files = touched.uniq > end > task :test_recent => [ :clone_development_structure_to_test ] > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
This rake task is pure genius. Are you going to submit a patch for "rake recent" or should I? On Thu, 03 Feb 2005 09:57:04 -0800, Jeremy Kemper <jeremy-w7CzD/W5Ocjk1uMJSBkQmQ@public.gmane.org> wrote:> Rob Lally wrote: > > I''m having a problem setting up a fixture for a has_and_belongs_to_many > > relationship. > > > > - I create a join table (in this case people_projects) > > - I add the ''has_and_belongs_to'' to both of the classes > > - I create a people_projects.yaml file > > - I require fixtures people, project and people_project > > > > Everything runs fine until I put in the people_project fixture; then it > > complains about not being able to find a PersonProject constant. I > > assume it is looking for the class so that it can instantiate the > > objects to make them available to the test class, which isn''t going to > > work. > > > > Any suggestions how I can work round this? > > Fixture load attempts to create convenient instance variables for each > fixture loaded. If a fixture doesn''t have a corresponding model class, > it fails. I never use these instance variables, so I had commented out > the line that creates them. Here''s a patch that keeps the variables but > only loads them if the corresponding model class exists: > > http://dev.rubyonrails.com/ticket/572 > > Calling find on each fixture for every test case really puts the > molasses on your test suite. It''d be nice to be able to turn this > feature off entirely -- who''s using it, in any case? > > On a test-speed tangent, here''s a nice hack for your Rakefile. It > checks your files'' mtimes and only runs tests on anything that''s changed > in the last 10 minutes. Make it the default task to encourage frequent, > speedy testing. > > Best, > jeremy > > # Look up tests for recently modified sources. > def recent_tests(source_pattern, test_path, touched_since = 10.minutes.ago) > FileList[source_pattern].map do |path| > if File.mtime(path) > touched_since > test = "#{test_path}/#{File.basename(path, ''.rb'')}_test.rb" > test if File.exists?(test) > end > end.compact > end > > desc ''Test recent changes.'' > Rake::TestTask.new(''test_recent'') do |t| > since = 10.minutes.ago > touched = FileList[''test/**/*_test.rb''].select { |path| > File.mtime(path) > since } + > recent_tests(''app/models/*.rb'', ''test/unit'', since) + > recent_tests(''app/controllers/*.rb'', ''test/functional'', since) > > if touched.empty? > mins = ((Time.now - since).to_f / 60).to_i > raise SystemExit, "Nothing changed in the last #{mins} minutes. Get > to work!" > end > > t.libs << ''test'' > t.verbose = true > t.test_files = touched.uniq > end > task :test_recent => [ :clone_development_structure_to_test ] > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tobi http://www.snowdevil.ca - Snowboards which don''t suck http://www.hieraki.org - Open source book authoring http://blog.leetsoft.com - Technical weblog
I am playing with has_and_belongs_to_many association that required a join table to capture the association. I also learn about using "fixtures" to preload model into a database. However, is there a way to load the association table? Steve _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
> table to capture the association. I also learn about using "fixtures" to > preload model into a database. However, is there a way to load the > association table?Just create a fixtures file for the association table (like customers_clients.yml) and create the necessary associations in it. Then in your test case, add this file to the fixtures statement Jens-Christian
Wai-Ming S. Li wrote:> It does not work. Following the example in > http://manuals.rubyonrails.com/read/chapter/48 and create the > categories_documents.yml with one entry: > > association1: > category_id: 3 > document_id: 14 > > I added the fixture categories_documents to the document unit test and > got this error message > > 1) Error: > test_truth(DocumentTest): > NameError: uninitialized constant CategoriesDocumentThis is fixed in latest Rails: http://dev.rubyonrails.com/changeset/524 jeremy
It does not work. Following the example in http://manuals.rubyonrails.com/read/chapter/48 and create the categories_documents.yml with one entry: association1: category_id: 3 document_id: 14 I added the fixture categories_documents to the document unit test and got this error message 1) Error: test_truth(DocumentTest): NameError: uninitialized constant CategoriesDocument c:/ruby/lib/ruby/gems/1.8/gems/actionpack-1.4.0/lib/action_controller/support/dependencies.rb:59:in `const _missing'' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/fixtures.rb:297:in `const_get'' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/fixtures.rb:297:in `find'' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/fixtures.rb:149:in `instantiate_fixtur es'' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/fixtures.rb:149:in `each'' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/fixtures.rb:149:in `instantiate_fixtur es'' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/fixtures.rb:147:in `each_with_index'' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/fixtures.rb:147:in `each'' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/fixtures.rb:147:in `each_with_index'' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/fixtures.rb:147:in `instantiate_fixtur es'' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/fixtures.rb:359:in `instantiate_fixtur es'' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.6.0/lib/active_record/fixtures.rb:344:in `setup'' 1 tests, 0 assertions, 0 failures, 1 errors>From the error message, the fixture need a model to load into thedatabase. Steve Jens-Christian Fischer <jcf-m2zzWtmgR3gXXHkOk0aIfQ@public.gmane.org> Sent by: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org 02/07/2005 01:46 PM Please respond to rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org To Rails <rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org> cc Subject Re: [Rails] fixtures & has_and_belongs_to_many> table to capture the association. I also learn about using "fixtures" to > preload model into a database. However, is there a way to load the > association table?Just create a fixtures file for the association table (like customers_clients.yml) and create the necessary associations in it. Then in your test case, add this file to the fixtures statement Jens-Christian _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails