Debugging Active Record Tests

Hey!

I’m looking to make a change in Active Record, and I’m trying to understand what a piece of code is currently doing and how to change it. I’m running ruby 3.1 and have added a breakpoint to a piece of code using binding.break and I’m running the associated tests, but I get NoMethodError: undefined method ‘break' for #<Binding:0x000000010b1155b0>. I have also tried require 'debugger' before this, but then I get cannot load such file -- debugger. Tests run fine without the binding.break.

Any pointers? How do contributors usually step through and experiment with code? Happy to add a section to the “Contributing to Rails” docs if I can get some help!

Here are sample to use debug gem inside the Active Record module.

  • Add require 'debug' and binding.break` This location is just an example. Add these two lines where you want to debug.
$ git diff
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
index a37ae644f4..091767f809 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -90,6 +90,8 @@ def self.quoted_table_names # :nodoc:
       end

       def initialize(connection, logger = nil, config = {}) # :nodoc:
+        require 'debug'
+        binding.break
         super()

         @raw_connection      = connection
$
  • Run Active Record unit test that stops at the break point configured
$ cd activerecord
$ bin/test
$ bin/test
Using sqlite3
[89, 98] in ~/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
    89|         @quoted_table_names ||= {}
    90|       end
    91|
    92|       def initialize(connection, logger = nil, config = {}) # :nodoc:
    93|         require 'debug'
=>  94|         binding.break
    95|         super()
    96|
    97|         @raw_connection      = connection
    98|         @owner               = nil
=>#0	ActiveRecord::ConnectionAdapters::AbstractAdapter#initialize(connection=#<SQLite3::Database:0x00007f9215857df8 @..., logger=#<ActiveSupport::Logger:0x00007f9214e385..., config={:database=>"/home/yahonda/src/github.co...) at ~/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:94
  #1	ActiveRecord::ConnectionAdapters::SQLite3Adapter#initialize(connection=#<SQLite3::Database:0x00007f9215857df8 @..., logger=#<ActiveSupport::Logger:0x00007f9214e385..., connection_options=nil, config={:database=>"/home/yahonda/src/github.co...) at ~/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:90
  # and 25 frames (use `bt' command for all frames)
(rdbg) connection
#<SQLite3::Database:0x00007f9215857df8
 @authorizer=nil,
 @busy_handler=nil,
 @collations={},
 @encoding=nil,
 @functions={},
 @readonly=false,
 @results_as_hash=true,
 @tracefunc=nil,
 @type_translation=nil,
 @type_translator=
  #<Proc:0x00007f9215645ee8 /home/yahonda/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/sqlite3-1.4.2/lib/sqlite3/database.rb:722 (lambda)>>
(rdbg)

Without require 'debug', it raises undefined method break’ for #Binding:0x00007f149b7b7a30 (NoMethodError)`.

$ git diff
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
index a37ae644f4..183933defc 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -90,6 +90,8 @@ def self.quoted_table_names # :nodoc:
       end

       def initialize(connection, logger = nil, config = {}) # :nodoc:
+#        require 'debug'
+        binding.break
         super()

         @raw_connection      = connection
$ bin/test
Using sqlite3
/home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:94:in `initialize': undefined method `break' for #<Binding:0x00007f149b7b7a30> (NoMethodError)

        binding.break
               ^^^^^^
	from /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:90:in `initialize'
	from /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:40:in `new'
	from /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb:40:in `sqlite3_connection'
	from /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:656:in `public_send'
	from /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:656:in `new_connection'
	from /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:700:in `checkout_new_connection'
	from /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:679:in `try_to_checkout_new_connection'
	from /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:640:in `acquire_connection'
	from /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:341:in `checkout'
	from /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:181:in `connection'
	from /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_handler.rb:185:in `retrieve_connection'
	from /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/connection_handling.rb:287:in `retrieve_connection'
	from /home/yahonda/src/github.com/rails/rails/activerecord/lib/active_record/connection_handling.rb:254:in `connection'
	from /home/yahonda/src/github.com/rails/rails/activerecord/test/cases/helper.rb:31:in `<top (required)>'
	from /home/yahonda/src/github.com/rails/rails/activerecord/test/activejob/helper.rb:3:in `require'
	from /home/yahonda/src/github.com/rails/rails/activerecord/test/activejob/helper.rb:3:in `<top (required)>'
	from /home/yahonda/src/github.com/rails/rails/activerecord/test/activejob/destroy_association_async_test.rb:3:in `require'
	from /home/yahonda/src/github.com/rails/rails/activerecord/test/activejob/destroy_association_async_test.rb:3:in `<top (required)>'
	from /home/yahonda/src/github.com/rails/rails/railties/lib/rails/test_unit/runner.rb:48:in `require'
	from /home/yahonda/src/github.com/rails/rails/railties/lib/rails/test_unit/runner.rb:48:in `block in load_tests'
	from /home/yahonda/src/github.com/rails/rails/railties/lib/rails/test_unit/runner.rb:48:in `each'
	from /home/yahonda/src/github.com/rails/rails/railties/lib/rails/test_unit/runner.rb:48:in `load_tests'
	from /home/yahonda/src/github.com/rails/rails/railties/lib/rails/test_unit/runner.rb:41:in `run'
	from /home/yahonda/src/github.com/rails/rails/activerecord/test/support/tools.rb:35:in `<top (required)>'
	from bin/test:11:in `require_relative'
	from bin/test:11:in `<main>'
$

Ah, debug not debugger! Thanks Yasuo.

1 Like