I want to setup a test that checks for a given method that that method will use the right replica role (using the Multiple Database feature by ActiveSupport since Rails 6.1)
Let's pretend that we have this database configuration:
# config/database.yml
default: &default
# (omissis)
test:
primary:
<<: *default
database: primary_db
primary_replica:
<<: *default
database: reader_db # for testing purpose, could be the same primary_db...
replica: true
the default writing and reading database roles that Rails conventionally would expect to be present and this class:
class MyService
def call_to_primary
Model.where(id: 1)
end
def call_to_reader
ActiveRecord::Base.connected_to(role: :reading) do
Model.where(id: 1)
end
end
end
I want to write a test to check that all the methods in the class above are using the reader replica and thus I'll expect that the test on the call_to_primary method will fail, while the test on the call_to_reader won't.
What is the right/best way to achieve this in RSpec?
Thank you!