rspec / rspec-rails

RSpec for Rails 7+
https://rspec.info
MIT License
5.19k stars 1.04k forks source link

undefined method `pool` when running a test with multiple databases on Rails 6.1 #2504

Closed jdarnok closed 3 years ago

jdarnok commented 3 years ago

Hey guys! just want to let you know about a bug I've encountered with multiple databases on Rails 6.1 I've found similar case when I explained what happened: https://github.com/rails/rails/issues/41549

Basically, when you have models that are fetched from different databases - you have to provide model spec - it can be empty, but it has to be there. I presume it's some loading feature of rspec, because setting config.eager_load = true in test environment fixes that

Way to reproduce - Just have models that are depending on connects_to database:. Then, if you try to run specs, but you don't have any model specs (let's say you want to run a controller spec) - you will encounter error from the title. Only works when you run tests globally though - when you try to run a single file (which is not a model test), it will fail again

gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    actioncable (6.1.3.2)
      actionpack (= 6.1.3.2)
      activesupport (= 6.1.3.2)
      nio4r (~> 2.0)
      websocket-driver (>= 0.6.1)
    actionmailbox (6.1.3.2)
      actionpack (= 6.1.3.2)
      activejob (= 6.1.3.2)
      activerecord (= 6.1.3.2)
      activestorage (= 6.1.3.2)
      activesupport (= 6.1.3.2)
      mail (>= 2.7.1)
    actionmailer (6.1.3.2)
      actionpack (= 6.1.3.2)
      actionview (= 6.1.3.2)
      activejob (= 6.1.3.2)
      activesupport (= 6.1.3.2)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 2.0)
    actionpack (6.1.3.2)
      actionview (= 6.1.3.2)
      activesupport (= 6.1.3.2)
      rack (~> 2.0, >= 2.0.9)
      rack-test (>= 0.6.3)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.0, >= 1.2.0)
    actiontext (6.1.3.2)
      actionpack (= 6.1.3.2)
      activerecord (= 6.1.3.2)
      activestorage (= 6.1.3.2)
      activesupport (= 6.1.3.2)
      nokogiri (>= 1.8.5)
    actionview (6.1.3.2)
      activesupport (= 6.1.3.2)
      builder (~> 3.1)
      erubi (~> 1.4)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.1, >= 1.2.0)
    activejob (6.1.3.2)
      activesupport (= 6.1.3.2)
      globalid (>= 0.3.6)
    activemodel (6.1.3.2)
      activesupport (= 6.1.3.2)
    activerecord (6.1.3.2)
      activemodel (= 6.1.3.2)
      activesupport (= 6.1.3.2)
    activerecord-postgres_enum (1.6.0)
      activerecord (>= 5, < 6.2)
      pg
    activestorage (6.1.3.2)
      actionpack (= 6.1.3.2)
      activejob (= 6.1.3.2)
      activerecord (= 6.1.3.2)
      activesupport (= 6.1.3.2)
      marcel (~> 1.0.0)
      mini_mime (~> 1.0.2)
    activesupport (6.1.3.2)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 1.6, < 2)
      minitest (>= 5.1)
      tzinfo (~> 2.0)
      zeitwerk (~> 2.3)
    addressable (2.7.0)
      public_suffix (>= 2.0.2, < 5.0)
    bindex (0.8.1)
    bootsnap (1.7.5)
      msgpack (~> 1.0)
    builder (3.2.4)
    byebug (11.1.3)
    capybara (3.35.3)
      addressable
      mini_mime (>= 0.1.3)
      nokogiri (~> 1.8)
      rack (>= 1.6.0)
      rack-test (>= 0.6.3)
      regexp_parser (>= 1.5, < 3.0)
      xpath (~> 3.2)
    childprocess (3.0.0)
    coderay (1.1.3)
    concurrent-ruby (1.1.8)
    crass (1.0.6)
    diff-lcs (1.4.4)
    doorkeeper (5.5.1)
      railties (>= 5)
    erubi (1.10.0)
    factory_bot (6.2.0)
      activesupport (>= 5.0.0)
    factory_bot_rails (6.2.0)
      factory_bot (~> 6.2.0)
      railties (>= 5.0.0)
    faker (2.18.0)
      i18n (>= 1.6, < 2)
    ffi (1.15.0)
    formatador (0.2.5)
    globalid (0.4.2)
      activesupport (>= 4.2.0)
    guard (2.17.0)
      formatador (>= 0.2.4)
      listen (>= 2.7, < 4.0)
      lumberjack (>= 1.0.12, < 2.0)
      nenv (~> 0.1)
      notiffany (~> 0.0)
      pry (>= 0.9.12)
      shellany (~> 0.0)
      thor (>= 0.18.1)
    guard-compat (1.2.1)
    guard-rspec (4.7.3)
      guard (~> 2.1)
      guard-compat (~> 1.1)
      rspec (>= 2.99.0, < 4.0)
    i18n (1.8.10)
      concurrent-ruby (~> 1.0)
    jbuilder (2.11.2)
      activesupport (>= 5.0.0)
    jsonapi-serializer (2.2.0)
      activesupport (>= 4.2)
    listen (3.5.1)
      rb-fsevent (~> 0.10, >= 0.10.3)
      rb-inotify (~> 0.9, >= 0.9.10)
    loofah (2.9.1)
      crass (~> 1.0.2)
      nokogiri (>= 1.5.9)
    lumberjack (1.2.8)
    mail (2.7.1)
      mini_mime (>= 0.1.1)
    marcel (1.0.1)
    method_source (1.0.0)
    mini_mime (1.0.3)
    minitest (5.14.4)
    msgpack (1.4.2)
    nenv (0.3.0)
    nio4r (2.5.7)
    nokogiri (1.11.3-x86_64-darwin)
      racc (~> 1.4)
    nokogiri (1.11.3-x86_64-linux)
      racc (~> 1.4)
    notiffany (0.1.3)
      nenv (~> 0.1)
      shellany (~> 0.0)
    pg (1.2.3)
    pry (0.14.1)
      coderay (~> 1.1)
      method_source (~> 1.0)
    pry-rails (0.3.9)
      pry (>= 0.10.4)
    public_suffix (4.0.6)
    puma (5.3.1)
      nio4r (~> 2.0)
    racc (1.5.2)
    rack (2.2.3)
    rack-mini-profiler (2.3.2)
      rack (>= 1.2.0)
    rack-proxy (0.6.5)
      rack
    rack-test (1.1.0)
      rack (>= 1.0, < 3)
    rails (6.1.3.2)
      actioncable (= 6.1.3.2)
      actionmailbox (= 6.1.3.2)
      actionmailer (= 6.1.3.2)
      actionpack (= 6.1.3.2)
      actiontext (= 6.1.3.2)
      actionview (= 6.1.3.2)
      activejob (= 6.1.3.2)
      activemodel (= 6.1.3.2)
      activerecord (= 6.1.3.2)
      activestorage (= 6.1.3.2)
      activesupport (= 6.1.3.2)
      bundler (>= 1.15.0)
      railties (= 6.1.3.2)
      sprockets-rails (>= 2.0.0)
    rails-dom-testing (2.0.3)
      activesupport (>= 4.2.0)
      nokogiri (>= 1.6)
    rails-html-sanitizer (1.3.0)
      loofah (~> 2.3)
    railties (6.1.3.2)
      actionpack (= 6.1.3.2)
      activesupport (= 6.1.3.2)
      method_source
      rake (>= 0.8.7)
      thor (~> 1.0)
    rake (13.0.3)
    rb-fsevent (0.11.0)
    rb-inotify (0.10.1)
      ffi (~> 1.0)
    regexp_parser (2.1.1)
    rexml (3.2.5)
    rspec (3.10.0)
      rspec-core (~> 3.10.0)
      rspec-expectations (~> 3.10.0)
      rspec-mocks (~> 3.10.0)
    rspec-core (3.10.1)
      rspec-support (~> 3.10.0)
    rspec-expectations (3.10.1)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.10.0)
    rspec-mocks (3.10.2)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.10.0)
    rspec-rails (5.0.1)
      actionpack (>= 5.2)
      activesupport (>= 5.2)
      railties (>= 5.2)
      rspec-core (~> 3.10)
      rspec-expectations (~> 3.10)
      rspec-mocks (~> 3.10)
      rspec-support (~> 3.10)
    rspec-support (3.10.2)
    rubyzip (2.3.0)
    sass-rails (6.0.0)
      sassc-rails (~> 2.1, >= 2.1.1)
    sassc (2.4.0)
      ffi (~> 1.9)
    sassc-rails (2.1.2)
      railties (>= 4.0.0)
      sassc (>= 2.0)
      sprockets (> 3.0)
      sprockets-rails
      tilt
    selenium-webdriver (3.142.7)
      childprocess (>= 0.5, < 4.0)
      rubyzip (>= 1.2.2)
    semantic_range (3.0.0)
    shellany (0.0.1)
    spring (2.1.1)
    sprockets (4.0.2)
      concurrent-ruby (~> 1.0)
      rack (> 1, < 3)
    sprockets-rails (3.2.2)
      actionpack (>= 4.0)
      activesupport (>= 4.0)
      sprockets (>= 3.0.0)
    thor (1.1.0)
    tilt (2.0.10)
    turbolinks (5.2.1)
      turbolinks-source (~> 5.2)
    turbolinks-source (5.2.0)
    tzinfo (2.0.4)
      concurrent-ruby (~> 1.0)
    web-console (4.1.0)
      actionview (>= 6.0.0)
      activemodel (>= 6.0.0)
      bindex (>= 0.4.0)
      railties (>= 6.0.0)
    webdrivers (4.6.0)
      nokogiri (~> 1.6)
      rubyzip (>= 1.3.0)
      selenium-webdriver (>= 3.0, < 4.0)
    webpacker (5.3.0)
      activesupport (>= 5.2)
      rack-proxy (>= 0.6.1)
      railties (>= 5.2)
      semantic_range (>= 2.3.0)
    websocket-driver (0.7.3)
      websocket-extensions (>= 0.1.0)
    websocket-extensions (0.1.5)
    xpath (3.2.0)
      nokogiri (~> 1.8)
    zeitwerk (2.4.2)

PLATFORMS
  x86_64-darwin-20
  x86_64-linux

DEPENDENCIES
  activerecord-postgres_enum
  bootsnap (>= 1.4.4)
  byebug
  capybara (>= 3.26)
  doorkeeper
  factory_bot_rails
  faker
  guard-rspec
  jbuilder (~> 2.7)
  jsonapi-serializer
  listen (~> 3.3)
  pg (~> 1.1)
  pry-rails
  puma (~> 5.0)
  rack-mini-profiler (~> 2.0)
  rails (~> 6.1.3, >= 6.1.3.2)
  rexml (~> 3.2, >= 3.2.4)
  rspec-rails (~> 5.0.0)
  sass-rails (>= 6)
  selenium-webdriver
  spring
  turbolinks (~> 5)
  tzinfo-data
  web-console (>= 4.1.0)
  webdrivers
  webpacker (~> 5.0)

RUBY VERSION
   ruby 3.0.1p64

BUNDLED WITH
   2.2.17
zzak commented 3 years ago

As I commented earlier, I'm not sure this isn't just a bug with rspec: https://github.com/rails/rails/issues/41549#issuecomment-851527738

zzak commented 3 years ago

🤦 Wow, whoops I thought this was the rails tracker... sorry!

pirj commented 3 years ago

Do you mind providing a sample application that reproduces the failure?

My reproduction attempt did not succeed so far:

mkdir rails-multi-pool
cd rails-multi-pool
echo "source 'https://rubygems.org\ngem 'rails'"
rvm use --create 2.7.2@rails-multi-pool
bundle
bundle exec rails new . --no-rc --skip-javascript --skip-bootsnap --skip-sprockets --skip-git --skip-test-unit --skip-listen --skip-bundle --skip-spring
echo "group :development, :test do\ngem 'rspec-rails'\nend" >> Gemfile
bundle
bundle exec rails generate rspec:install

config/database.yml:

default: &default
  adapter: sqlite3
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000

development:
  primary:
    <<: *default
    database: db/development.sqlite3
  secondary:
    <<: *default
    database: db/development2.sqlite3

test:
  primary:
    <<: *default
    database: db/test.sqlite3
  secondary:
    <<: *default
    database: db/test2.sqlite3
bundle exec rails generate model user
bundle exec rails generate model item --database=secondary
bundle exec rails generate controller hello
bundle exec rails db:migrate RAILS_ENV=test
$ rspec
4 examples, 0 failures, 4 pending
$ rm spec/models/*
$ rspec
2 examples, 0 failures, 2 pending

I've removed the following line from config/database.yml:

  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
$ rspec
2 examples, 0 failures, 2 pending
JonRowe commented 3 years ago

Lets move discussion back to the Rails issue, I'm 95% confident this is a Rails loading issue.