How can I run AR gem tests?

Hello,

I want to contribute to ActiveRecord source code by fixing an issue. Tried to create a test that will reproduce the issue. I followed the steps in Contributing to Ruby on Rails — Ruby on Rails Guides. Unfortunately the existing AR tests do not pass for me.

bundle exec rake db:postgresql:build

has built a new database for me but it does not have any tables. As a result, when I run rake test:postgresql in activerecord, I get errors like

Traceback (most recent call last):
	28: from /Users/ylapitsky/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/rake_test_loader.rb:5:in `<main>'
	27: from /Users/ylapitsky/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/rake_test_loader.rb:5:in `select'
	26: from /Users/ylapitsky/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/rake_test_loader.rb:17:in `block in <main>'
	25: from /Users/ylapitsky/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/rake-13.0.1/lib/rake/rake_test_loader.rb:17:in `require'
	24: from /Users/ylapitsky/projects/patientiq/rails/activerecord/test/cases/adapter_prevent_writes_test.rb:3:in `<top (required)>'
	23: from /Users/ylapitsky/projects/patientiq/rails/activerecord/test/cases/adapter_prevent_writes_test.rb:3:in `require'
	22: from /Users/ylapitsky/projects/patientiq/rails/activerecord/test/cases/helper.rb:14:in `<top (required)>'
	21: from /Users/ylapitsky/projects/patientiq/rails/activesupport/lib/active_support/dependencies.rb:332:in `require'
	20: from /Users/ylapitsky/projects/patientiq/rails/activesupport/lib/active_support/dependencies.rb:299:in `load_dependency'
	19: from /Users/ylapitsky/projects/patientiq/rails/activesupport/lib/active_support/dependencies.rb:332:in `block in require'
	18: from /Users/ylapitsky/projects/patientiq/rails/activesupport/lib/active_support/dependencies.rb:332:in `require'
	17: from /Users/ylapitsky/projects/patientiq/rails/activerecord/test/support/connection.rb:4:in `<top (required)>'
	16: from /Users/ylapitsky/projects/patientiq/rails/activesupport/lib/active_support/dependencies.rb:332:in `require'
	15: from /Users/ylapitsky/projects/patientiq/rails/activesupport/lib/active_support/dependencies.rb:299:in `load_dependency'
	14: from /Users/ylapitsky/projects/patientiq/rails/activesupport/lib/active_support/dependencies.rb:332:in `block in require'
	13: from /Users/ylapitsky/projects/patientiq/rails/activesupport/lib/active_support/dependencies.rb:332:in `require'
	12: from /Users/ylapitsky/projects/patientiq/rails/activerecord/test/models/college.rb:6:in `<top (required)>'
	11: from /Users/ylapitsky/projects/patientiq/rails/activerecord/test/models/college.rb:7:in `<class:College>'
	10: from /Users/ylapitsky/projects/patientiq/rails/activerecord/lib/active_record/associations.rb:1458:in `has_many'
	 9: from /Users/ylapitsky/projects/patientiq/rails/activerecord/lib/active_record/associations/builder/association.rb:34:in `build'
	 8: from /Users/ylapitsky/projects/patientiq/rails/activerecord/lib/active_record/associations/builder/collection_association.rb:14:in `define_callbacks'
	 7: from /Users/ylapitsky/projects/patientiq/rails/activerecord/lib/active_record/associations/builder/association.rb:81:in `define_callbacks'
	 6: from /Users/ylapitsky/projects/patientiq/rails/activerecord/lib/active_record/associations/builder/association.rb:81:in `each'
	 5: from /Users/ylapitsky/projects/patientiq/rails/activerecord/lib/active_record/associations/builder/association.rb:82:in `block in define_callbacks'
	 4: from /Users/ylapitsky/projects/patientiq/rails/activerecord/lib/active_record/autosave_association.rb:143:in `build'
	 3: from /Users/ylapitsky/projects/patientiq/rails/activerecord/lib/active_record/autosave_association.rb:229:in `add_autosave_association_callbacks'
	 2: from /Users/ylapitsky/projects/patientiq/rails/activerecord/lib/active_record/autosave_association.rb:243:in `define_autosave_validation_callbacks'
	 1: from /Users/ylapitsky/projects/patientiq/rails/activemodel/lib/active_model/validations/callbacks.rb:95:in `after_validation'
/Users/ylapitsky/projects/patientiq/rails/activerecord/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined method `set_options_for_callback' for College (call 'College.connection' to establish a connection):Class (NoMethodError)
Did you mean?  set_options_for_callbacks!

I think it happens because the tests rely on fixtures for tables like colleges but there are no tables in the database.

Please advise.

Thank you, Yuri

Please, have a look at GitHub - rails/rails-dev-box: A virtual machine for Ruby on Rails core development.

I think it will work if you prefix the task with db:postgresql:rebuild to setup the PostgreSQL database:

cd rails/activerecord && bundle exec rake db:postgresql:rebuild postgresql:test

and for the isolated test:

cd rails/activerecord && bundle exec rake db:postgresql:rebuild postgresql: isolated_test

Thank you!

This is what I did. Tried it again, still get

...
	12: from test/models/college.rb:6:in `<top (required)>'
	11: from test/models/college.rb:7:in `<class:College>'
	10: from lib/active_record/associations.rb:1458:in `has_many'
	 9: from lib/active_record/associations/builder/association.rb:34:in `build'
	 8: from lib/active_record/associations/builder/collection_association.rb:14:in `define_callbacks'
	 7: from lib/active_record/associations/builder/association.rb:81:in `define_callbacks'
	 6: from lib/active_record/associations/builder/association.rb:81:in `each'
	 5: from lib/active_record/associations/builder/association.rb:82:in `block in define_callbacks'
	 4: from lib/active_record/autosave_association.rb:143:in `build'
	 3: from lib/active_record/autosave_association.rb:229:in `add_autosave_association_callbacks'
	 2: from lib/active_record/autosave_association.rb:243:in `define_autosave_validation_callbacks'
	 1: from lib/active_model/validations/callbacks.rb:95:in `after_validation'
lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined method `set_options_for_callback' for College (call 'College.connection' to establish a connection):Class (NoMethodError)
Did you mean?  set_options_for_callbacks!
rake aborted!

db:postgresql:rebuild builds two empty dbs. I will probably need to follow the recommended setup with VirtualBox and Vagrant.

Thank you,

I will follow the suggestion

Are there any changes and what git sha are you on?

I just cloned the repo ran bundle and bundle exec rake db:postgresql:rebuild postgresql:test they are passing for me.

set_options_for_callbacks should be defined right under where it’s called by looking at the stack trace.

Either way VirtualBox and Vagrant always a good option :+1:

Thank you a lot!

Actually when I pulled the most recent version the problem disappeared. I cloned the repo some day on the last week of Dec 2020. Probably there were some changes that fixed the issue.

Thank you again!