Skip to content

Checking in tests/specs that READs actually go to the replica #182

@serggiu

Description

@serggiu

First of all, this is a great gem, thank you for sharing it with us!

I've added it in an app and it's working great, though I'm trying to validate with some specs that READs actually go to the read replica, and it doesn't seem to make this switch automatically in a test environment.

Here's a simple spec, please let me know if specific configuration is needed to make the gem work in a test environment:

  it "fetches from the read replica" do
      expect { User.count }.to exactly_query_databases({ primary_replica: :reading })
  end

and I get back { primary: :writing }, not { primary_replica: :reading }

If I manually switch to the read replica, then I get back what I'm expecting:

it "fetches from the read replica" do
    ActiveRecord::Base.connected_to(role: :reading) do
      expect { User.count }.to exactly_query_databases({ primary_replica: :reading })
    end
  end

I must be missing something though it's not obvious to me.

Thank you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions