Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WrongScopeError: name when use_active_record = false #2417

Closed
matiasgarcia opened this issue Dec 15, 2020 · 43 comments · Fixed by #2423
Closed

WrongScopeError: name when use_active_record = false #2417

matiasgarcia opened this issue Dec 15, 2020 · 43 comments · Fixed by #2423
Assignees
Milestone

Comments

@matiasgarcia
Copy link

I have the following spec:

require 'rails_helper'

describe Command do
  it 'foo' do
    Command.new
  end
end

Using RSpec 3.9.1 + Rails 6.1 I get:

RSpec::Core::ExampleGroup::WrongScopeError: `name` is not available from within an example (e.g. an `it` block) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). It is only available on an example group (e.g. a `describe` or `context` block).

  0) Command foo
     Failure/Error:
       raise WrongScopeError,
             "`#{name}` is not available from within an example (e.g. an " \
             "`it` block) or from constructs that run in the scope of an " \
             "example (e.g. `before`, `let`, etc). It is only available " \
             "on an example group (e.g. a `describe` or `context` block)."

       `name` is not available from within an example (e.g. an `it` block) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc). It is only available on an example group (e.g. a `describe` or `context` block).
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/example_group.rb:746:in `method_missing'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-6.1.0/lib/active_record/test_fixtures.rb:102:in `run_in_transaction?'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-6.1.0/lib/active_record/test_fixtures.rb:116:in `setup_fixtures'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/activerecord-6.1.0/lib/active_record/test_fixtures.rb:10:in `before_setup'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-rails-3.9.0/lib/rspec/rails/adapters.rb:126:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/example.rb:450:in `instance_exec'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/example.rb:450:in `instance_exec'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/hooks.rb:381:in `execute_with'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/hooks.rb:614:in `block (2 levels) in run_around_example_hooks_for'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/example.rb:345:in `call'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/hooks.rb:615:in `run_around_example_hooks_for'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/hooks.rb:472:in `run'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/example.rb:460:in `with_around_example_hooks'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/example.rb:503:in `with_around_and_singleton_context_hooks'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/example.rb:254:in `run'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/example_group.rb:633:in `block in run_examples'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/example_group.rb:629:in `map'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/example_group.rb:629:in `run_examples'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/example_group.rb:595:in `run'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/runner.rb:121:in `block (3 levels) in run_specs'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/runner.rb:121:in `map'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/runner.rb:121:in `block (2 levels) in run_specs'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/configuration.rb:2031:in `with_suite_hooks'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/runner.rb:116:in `block in run_specs'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/reporter.rb:74:in `report'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/runner.rb:115:in `run_specs'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/runner.rb:89:in `run'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/runner.rb:71:in `run'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/lib/rspec/core/runner.rb:45:in `invoke'
     # /home/mgarcia/.rbenv/versions/2.5.7/lib/ruby/gems/2.5.0/gems/rspec-core-3.9.1/exe/rspec:4:in `<top (required)>'
     # /home/mgarcia/.rbenv/versions/2.5.7/bin/rspec:23:in `load'
     # /home/mgarcia/.rbenv/versions/2.5.7/bin/rspec:23:in `<main>'
     # 
     #   Showing full backtrace because every line was filtered out.
     #   See docs for RSpec::Configuration#backtrace_exclusion_patterns and
     #   RSpec::Configuration#backtrace_inclusion_patterns for more information.
@pirj
Copy link
Member

pirj commented Dec 15, 2020

Did it work on prior versions of Rails, or is it a new project?

I see a couple weird things here.

 0) Command foo

was it really 0?

Kindly appreciate if you could test your setup against rspec-rails 4.0.1 and also branch rails-6-1-dev (see PR #2398) by setting the following in your Gemfile:

gem 'rspec-rails`, github: 'rspec/rspec-rails', branch: 'rails-6-1-dev'

Yet another approach would be to quote Command:

RSpec.describe 'Command' do

I'd love to see Command's source to make an informed judgement.

@amilligan
Copy link

I can verify this is definitely a problem with Rails 6.1 compatibility. We make heavy use of the latest released rspec-rails (4.0.1) with the previous version of Rails (6.0.3.4) with no problem. Upgrading to Rails 6.1 causes the above error over and over.

We haven't been able to try the rails-6-1-dev branch, because of dependency hell. Bundler gives us a whole bunch of this:

Bundler could not find compatible versions for gem "rspec-support":
  In snapshot (Gemfile.lock):
    rspec-support (= 3.10.0)

  In Gemfile:
    rspec-collection_matchers x86_64-darwin-19 was resolved to 1.2.0, which depends on
      rspec-expectations (>= 2.99.0.beta1) x86_64-darwin-19 was resolved to 3.10.0, which depends on
        rspec-support (~> 3.10.0) x86_64-darwin-19

    rspec-rails x86_64-darwin-19 was resolved to 4.1.0.pre, which depends on
      rspec-support (= 3.11.0.pre)

Running `bundle update` will rebuild your snapshot from scratch, using only
the gems in your Gemfile, which may resolve the conflict.

I actually came across the pull request you referenced above (#2398) before this issue. It looks like that will solve this; it would be great to get a pre-release gem out.

@pirj
Copy link
Member

pirj commented Dec 17, 2020

You'll have to point to unreleased branches of other repos (core, support, expectations, mocks) as well:

gem 'rspec-support`, github: 'rspec/rspec-support', branch: 'main'

There's a lot of development going on ATM, no promises on pre-release versions, sorry.
Stay tuned for 4.1.

@pirj pirj closed this as completed Dec 17, 2020
@pirj
Copy link
Member

pirj commented Dec 17, 2020

Really appreciate if you could check if you are still experiencing this problem against 4.1.0.pre and 3.11.0.pre. Please feel free to reopen the ticket in this case.

@JonRowe JonRowe reopened this Dec 17, 2020
@JonRowe
Copy link
Member

JonRowe commented Dec 17, 2020

Reopened because I want to investigate, I haven't seen anything like this so far.

@JonRowe
Copy link
Member

JonRowe commented Dec 18, 2020

I'm going to investigate over the weekend and look to releasing a new version with 6.1 support, but can someone provide me with a spec example for this? The one above would be fine if the source for Command is provided.

@bmulholland
Copy link

@JonRowe A blank class will do:

require 'rails_helper'

class Command; end

RSpec.describe Command do
  it 'foo' do
    Command.new
  end
end

@matiasgarcia
Copy link
Author

matiasgarcia commented Dec 21, 2020

The solution for this was upgrading to rspec-rails 4.0.1.
According to the changelog on 4.x.x was introduced the compatibility with Rails 6.x.

I was confused because as a dev I would expect this version to not be downloadable for Rails 6.1.

@matiasgarcia
Copy link
Author

matiasgarcia commented Dec 21, 2020

@pirj It might be zero because the spec doesn't even run, it's a bug. Command is an AR model.

class Command < ApplicationRecord
end

@pirj
Copy link
Member

pirj commented Dec 21, 2020

There's no good way to prevent us from future breakages due to incompatibilities introduced by Rails. E.g. you can use rspec-rails 2.99 with Rails 6.1, but it doesn't mean you should, since most probably it won't work.

@bmulholland
Copy link

Upgrading to rspec-rails 4.0.1 does not resolve this issue for me.

If there's a tight connection with rails, perhaps there should be a corresponding restriction on what versions of Rails this gem can be installed with?

@pirj
Copy link
Member

pirj commented Dec 21, 2020

There is this https://github.com/rspec/rspec-rails/blob/main/features/README.md#rails

rspec-rails 4 supports Rails 5.x and 6.x. For earlier versions of Rails, you need rspec-rails 3

We could set a hard constraint on Rails version, e.g. to ~> 6.0.0 for rspec-rails 4.0.x, but that would:

  1. force to update to newer rspec-rails along with Rails update
  2. prevent users from updating Rails early and submit issues and patches, leaving all the burden on maintainers

@bmulholland
Copy link

@pirj Are there special instructions for installing the preview versions? I'm getting Could not find gem 'rspec-rails (= 4.1.0.pre)' in any of the gem sources listed in your Gemfile.

@pirj
Copy link
Member

pirj commented Dec 21, 2020

@bmulholland You typically use github source in your Gemfile and point to a branch:

gem 'rspec-rails', github: 'rspec/rspec-rails', branch: 'rails-6-1-dev'

@bmulholland
Copy link

@pirj Thanks! Your message above sounded like you'd published a package.

I can't install that branch:

Bundler could not find compatible versions for gem "rspec-core":
  In Gemfile:
    guard-rspec was resolved to 4.7.3, which depends on
      rspec (>= 2.99.0, < 4.0) was resolved to 3.10.0, which depends on
        rspec-core (~> 3.10.0)

    rspec-rails was resolved to 4.1.0.pre, which depends on
      rspec-core (= 3.11.0.pre)

    rspec_junit_formatter was resolved to 0.4.1, which depends on
      rspec-core (>= 2, < 4, != 2.12.0)

Could not find gem 'rspec-core (= 3.11.0.pre)', which is required by gem 'rspec-rails', in any of the sources.

It seems like there's a magic incantation to install these preview builds. Sounds like that's all obvious to you, but I don't have the same context :). I'm trying to help out with your request, above, to try out the preview build - can you make that easy for me? Thanks!

@pirj
Copy link
Member

pirj commented Dec 21, 2020

I couldn't reproduce the issue with this snippet (spin-off of this snippet recommended to submit Rails bug reports):

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "rails", "~> 6.1.0"
  gem "rspec-rails", "4.0.1"
  gem "sqlite3"
end

require "active_record"
require "logger"

require 'rspec/autorun'

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :commands, force: true do |t|
  end
end

class Command < ActiveRecord::Base
end

RSpec.describe Command do
  it 'foo' do
    Command.new
  end
end
$ ruby rr.rb
Fetching gem metadata from https://rubygems.org/.............
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Using rake 13.0.3
Using concurrent-ruby 1.1.7
Using i18n 1.8.5
Using minitest 5.14.2
Using tzinfo 2.0.4
Using zeitwerk 2.4.2
Using activesupport 6.1.0
Using builder 3.2.4
Using erubi 1.10.0
Using mini_portile2 2.4.0
Using nokogiri 1.10.10
Using rails-dom-testing 2.0.3
Using crass 1.0.6
Using loofah 2.8.0
Using rails-html-sanitizer 1.3.0
Using actionview 6.1.0
Using rack 2.2.3
Using rack-test 1.1.0
Using actionpack 6.1.0
Fetching nio4r 2.5.4
Installing nio4r 2.5.4 with native extensions
Fetching websocket-extensions 0.1.5
Installing websocket-extensions 0.1.5
Fetching websocket-driver 0.7.3
Installing websocket-driver 0.7.3 with native extensions
Fetching actioncable 6.1.0
Installing actioncable 6.1.0
Fetching globalid 0.4.2
Installing globalid 0.4.2
Fetching activejob 6.1.0
Installing activejob 6.1.0
Using activemodel 6.1.0
Using activerecord 6.1.0
Fetching mimemagic 0.3.5
Installing mimemagic 0.3.5
Fetching marcel 0.3.3
Installing marcel 0.3.3
Fetching activestorage 6.1.0
Installing activestorage 6.1.0
Fetching mini_mime 1.0.2
Installing mini_mime 1.0.2
Fetching mail 2.7.1
Installing mail 2.7.1
Fetching actionmailbox 6.1.0
Installing actionmailbox 6.1.0
Fetching actionmailer 6.1.0
Installing actionmailer 6.1.0
Fetching actiontext 6.1.0
Installing actiontext 6.1.0
Using bundler 2.1.4
Using diff-lcs 1.4.4
Using method_source 1.0.0
Using thor 1.0.1
Using railties 6.1.0
Fetching sprockets 4.0.2
Installing sprockets 4.0.2
Fetching sprockets-rails 3.2.2
Installing sprockets-rails 3.2.2
Fetching rails 6.1.0
Installing rails 6.1.0
Using rspec-support 3.10.0
Using rspec-core 3.10.0
Using rspec-expectations 3.10.0
Using rspec-mocks 3.10.0
Using rspec-rails 4.0.1
Using sqlite3 1.4.2
-- create_table(:commands, {:force=>true})
D, [2020-12-21T19:52:41.961032 #87338] DEBUG -- :    (4.4ms)  SELECT sqlite_version(*)
D, [2020-12-21T19:52:41.962427 #87338] DEBUG -- :    (0.2ms)  DROP TABLE IF EXISTS "commands"
D, [2020-12-21T19:52:41.969918 #87338] DEBUG -- :    (7.1ms)  CREATE TABLE "commands" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL)
   -> 0.0553s
D, [2020-12-21T19:52:42.092779 #87338] DEBUG -- :    (0.2ms)  CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime(6) NOT NULL, "updated_at" datetime(6) NOT NULL)
D, [2020-12-21T19:52:42.140222 #87338] DEBUG -- :   ActiveRecord::InternalMetadata Load (0.3ms)  SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = ? LIMIT ?  [["key", "environment"], ["LIMIT", 1]]
D, [2020-12-21T19:52:42.150361 #87338] DEBUG -- :   TRANSACTION (0.1ms)  begin transaction
D, [2020-12-21T19:52:42.151183 #87338] DEBUG -- :   ActiveRecord::InternalMetadata Create (0.3ms)  INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["key", "environment"], ["value", "development"], ["created_at", "2020-12-21 16:52:42.149787"], ["updated_at", "2020-12-21 16:52:42.149787"]]
D, [2020-12-21T19:52:42.152184 #87338] DEBUG -- :   TRANSACTION (0.2ms)  commit transaction
.

Finished in 0.02363 seconds (files took 1.86 seconds to load)
1 example, 0 failures

I'm going to investigate this further, it seems that there might be a problem with shadowing name here.

@pirj
Copy link
Member

pirj commented Dec 21, 2020

@bmulholland This should fix the dependencies:

  gem 'rspec-rails', github: 'rspec/rspec-rails', branch: 'rails-6-1-dev'
  %w[rspec-core rspec-mocks rspec-support rspec-expectations].each do |name|
    gem name, github: "rspec/#{name}", branch: 'main'
  end

@rhymes
Copy link

rhymes commented Dec 21, 2020

@bmulholland the issue there is the other gems: guard-rspec does not support rspec-core ~> 4, which is understandable because the gem hasn't seen a new release since 2016: https://rubygems.org/gems/guard-rspec

Same goes for rspec_junit_formatter which seems to be stuck in 2018.

Unfortunately if you still want to use those two packages you'll have to fork them and update them or else

@bmulholland
Copy link

@rhymes Oh good point. Okay, I'll have to look into that before I can try out the sample branch, so it will have to wait. Thanks all.

@pirj
Copy link
Member

pirj commented Dec 21, 2020

@rhymes Thanks for sharing!

Not sure what breaks those two exactly, since the major version of rspec-core version remained the same, 3.x.

guard-rspec was mostly unnecessary anyway, since the same can be achieved with guard-shell with a small script to run rspec when spec/ files change.

rspec_junit_formatter wasn't released in a while, but the repository receives periodic updates. Any luck using it from its master branch, @bmulholland, @matiasgarcia ?

gem 'rspec_junit_formatter`, github: 'sj26/rspec_junit_formatter', branch: 'master'

@bmulholland
Copy link

I'm just about to head out for the day, so I tried dropping the dependencies on rspec_junit_formatter and guard-rspec, and replace gem "rspec-rails" with:

  # Try early version of rspec that supports rails 6.1
  gem "rspec-rails", github: "rspec/rspec-rails", branch: "rails-6-1-dev"
  %w(rspec-core rspec-mocks rspec-support rspec-expectations).each do |name|
    gem name, github: "rspec/#{name}", branch: "main"
  end

Then upgraded to Rails 6.1 and re-ran rspec. rails-rspec commit is 4e88eb32b47984b6318f67f85a2f7122cf0e6591

Unfortunately, I'm still getting the same error raise WrongScopeError etc

It's possible I'm missing some of the rails 6.1 upgrade instructions, though I'd expect that to at least show a more helpful error.

@pirj
Copy link
Member

pirj commented Dec 21, 2020

@Edouard-chin, can you please take a look? Seems related to #2215 and rails/rails#37770

@pirj
Copy link
Member

pirj commented Dec 22, 2020

@bmulholland Any update? Can you reproduce on a fresh Rails app?

@bmulholland
Copy link

bmulholland commented Dec 22, 2020

@pirj It did not happen on a fresh Rails app so I spent some time tracking it down. To reproduce this error, set config.use_active_record = false in spec/rails_helper.rb. If you want a full-blown repro: https://github.com/bmulholland/rspec-rails-6-1 - just clone, bundle install, and run rspec.

Annoyingly, I am actually using AR in my project, although I wasn't when I first started the app. So what would be perhaps a workaround for others (enable AR support) is actually the full fix for me :)

@pirj
Copy link
Member

pirj commented Dec 22, 2020

Thanks a lot, @bmulholland!
That explains the error 👍
Glad it helped. Let me quickly see if it can be fixed to avoid future confusion.

@pirj
Copy link
Member

pirj commented Dec 22, 2020

@matiasgarcia @amilligan Do you happen to have use_active_record set to false in RSpec config?

@pirj pirj self-assigned this Dec 22, 2020
@vineethkv96
Copy link

Upgrade to rspec-rails 4.0.1 works for me.
Thanks

@silviorelli
Copy link

Hi all,
I got the same problems after updating a project from Rails 6.0 to Rails 6.1 and I can confirm that commenting out config.use_active_record = false solves them.

I also tried using the development version of the gems directly from Github as suggested (rspec-rails on rails-6-1-dev branch and rspec-core/mocks/expectations/support on main) but those are still affected by the issue.

@samstickland

This comment has been minimized.

@pirj

This comment has been minimized.

@pirj pirj added this to the 4.1 milestone Dec 24, 2020
@pirj pirj changed the title Incompatibility with Rails 6.1? WrongScopeError: name when use_active_record = false Dec 24, 2020
@matiasgarcia
Copy link
Author

@matiasgarcia @amilligan Do you happen to have use_active_record set to false in RSpec config?

No, this was a fresh app.

@matiasgarcia
Copy link
Author

matiasgarcia commented Dec 28, 2020

Just to give more context.

Click for code files, hidden by @JonRowe for brevity in thread.

Part of my Gemfile:

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.1.0'
# Use mysql as the database for Active Record
gem 'mysql2', '~> 0.5'
# Use Puma as the app server
gem 'puma', '~> 5.0'
# Use SCSS for stylesheets
gem 'sass-rails', '>= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 5.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'rspec-rails'
end

gemlock

GEM
  remote: https://rubygems.org/
  specs:
    actioncable (6.1.0)
      actionpack (= 6.1.0)
      activesupport (= 6.1.0)
      nio4r (~> 2.0)
      websocket-driver (>= 0.6.1)
    actionmailbox (6.1.0)
      actionpack (= 6.1.0)
      activejob (= 6.1.0)
      activerecord (= 6.1.0)
      activestorage (= 6.1.0)
      activesupport (= 6.1.0)
      mail (>= 2.7.1)
    actionmailer (6.1.0)
      actionpack (= 6.1.0)
      actionview (= 6.1.0)
      activejob (= 6.1.0)
      activesupport (= 6.1.0)
      mail (~> 2.5, >= 2.5.4)
      rails-dom-testing (~> 2.0)
    actionpack (6.1.0)
      actionview (= 6.1.0)
      activesupport (= 6.1.0)
      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.0)
      actionpack (= 6.1.0)
      activerecord (= 6.1.0)
      activestorage (= 6.1.0)
      activesupport (= 6.1.0)
      nokogiri (>= 1.8.5)
    actionview (6.1.0)
      activesupport (= 6.1.0)
      builder (~> 3.1)
      erubi (~> 1.4)
      rails-dom-testing (~> 2.0)
      rails-html-sanitizer (~> 1.1, >= 1.2.0)
    activejob (6.1.0)
      activesupport (= 6.1.0)
      globalid (>= 0.3.6)
    activemodel (6.1.0)
      activesupport (= 6.1.0)
    activerecord (6.1.0)
      activemodel (= 6.1.0)
      activesupport (= 6.1.0)
    activestorage (6.1.0)
      actionpack (= 6.1.0)
      activejob (= 6.1.0)
      activerecord (= 6.1.0)
      activesupport (= 6.1.0)
      marcel (~> 0.3.1)
      mimemagic (~> 0.3.2)
    activesupport (6.1.0)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 1.6, < 2)
      minitest (>= 5.1)
      tzinfo (~> 2.0)
      zeitwerk (~> 2.3)
    bindex (0.8.1)
    bootsnap (1.5.1)
      msgpack (~> 1.0)
    builder (3.2.4)
    byebug (11.1.3)
    concurrent-ruby (1.1.7)
    crass (1.0.6)
    diff-lcs (1.4.4)
    erubi (1.10.0)
    ffi (1.13.1)
    globalid (0.4.2)
      activesupport (>= 4.2.0)
    i18n (1.8.5)
      concurrent-ruby (~> 1.0)
    jbuilder (2.10.1)
      activesupport (>= 5.0.0)
    listen (3.3.3)
      rb-fsevent (~> 0.10, >= 0.10.3)
      rb-inotify (~> 0.9, >= 0.9.10)
    loofah (2.8.0)
      crass (~> 1.0.2)
      nokogiri (>= 1.5.9)
    mail (2.7.1)
      mini_mime (>= 0.1.1)
    marcel (0.3.3)
      mimemagic (~> 0.3.2)
    method_source (1.0.0)
    mimemagic (0.3.5)
    mini_mime (1.0.2)
    mini_portile2 (2.4.0)
    minitest (5.14.2)
    msgpack (1.3.3)
    mysql2 (0.5.3)
    nio4r (2.5.4)
    nokogiri (1.10.10)
      mini_portile2 (~> 2.4.0)
    puma (5.1.1)
      nio4r (~> 2.0)
    rack (2.2.3)
    rack-mini-profiler (2.2.0)
      rack (>= 1.2.0)
    rack-proxy (0.6.5)
      rack
    rack-test (1.1.0)
      rack (>= 1.0, < 3)
    rails (6.1.0)
      actioncable (= 6.1.0)
      actionmailbox (= 6.1.0)
      actionmailer (= 6.1.0)
      actionpack (= 6.1.0)
      actiontext (= 6.1.0)
      actionview (= 6.1.0)
      activejob (= 6.1.0)
      activemodel (= 6.1.0)
      activerecord (= 6.1.0)
      activestorage (= 6.1.0)
      activesupport (= 6.1.0)
      bundler (>= 1.15.0)
      railties (= 6.1.0)
      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.0)
      actionpack (= 6.1.0)
      activesupport (= 6.1.0)
      method_source
      rake (>= 0.8.7)
      thor (~> 1.0)
    rake (13.0.1)
    rb-fsevent (0.10.4)
    rb-inotify (0.10.1)
      ffi (~> 1.0)
    rspec-core (3.9.1)
      rspec-support (~> 3.9.1)
    rspec-expectations (3.9.0)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.9.0)
    rspec-mocks (3.9.1)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.9.0)
    rspec-rails (3.9.0)
      actionpack (>= 3.0)
      activesupport (>= 3.0)
      railties (>= 3.0)
      rspec-core (~> 3.9.0)
      rspec-expectations (~> 3.9.0)
      rspec-mocks (~> 3.9.0)
      rspec-support (~> 3.9.0)
    rspec-support (3.9.2)
    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
    semantic_range (2.3.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.0.1)
    tilt (2.0.10)
    turbolinks (5.2.1)
      turbolinks-source (~> 5.2)
    turbolinks-source (5.2.0)
    tzinfo (2.0.3)
      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)
    webpacker (5.2.1)
      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)
    zeitwerk (2.4.2)

PLATFORMS
  ruby

DEPENDENCIES
  bootsnap (>= 1.4.4)
  byebug
  jbuilder (~> 2.7)
  listen (~> 3.3)
  mysql2 (~> 0.5)
  puma (~> 5.0)
  rack-mini-profiler (~> 2.0)
  rails (~> 6.1.0)
  rspec-rails
  sass-rails (>= 6)
  spring
  turbolinks (~> 5)
  tzinfo-data
  web-console (>= 4.1.0)
  webpacker (~> 5.0)

RUBY VERSION
   ruby 2.5.7p206

BUNDLED WITH
   1.17.3

rails_helper

# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'

require File.expand_path('../config/environment', __dir__)

# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
begin
  ActiveRecord::Migration.maintain_test_schema!
rescue ActiveRecord::PendingMigrationError => e
  puts e.to_s.strip
  exit 1
end
RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  # RSpec Rails can automatically mix in different behaviours to your tests
  # based on their file location, for example enabling you to call `get` and
  # `post` in specs under `spec/controllers`.
  #
  # You can disable this behaviour by removing the line below, and instead
  # explicitly tag your specs with their type, e.g.:
  #
  #     RSpec.describe UsersController, :type => :controller do
  #       # ...
  #     end
  #
  # The different available types are documented in the features, such as in
  # https://relishapp.com/rspec/rspec-rails/docs
  config.infer_spec_type_from_file_location!

  # Filter lines from Rails gems in backtraces.
  config.filter_rails_from_backtrace!
  # arbitrary gems may also be filtered via:
  # config.filter_gems_from_backtrace("gem name")
end

spec_helper

# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
  # rspec-expectations config goes here. You can use an alternate
  # assertion/expectation library such as wrong or the stdlib/minitest
  # assertions if you prefer.
  config.expect_with :rspec do |expectations|
    # This option will default to `true` in RSpec 4. It makes the `description`
    # and `failure_message` of custom matchers include text for helper methods
    # defined using `chain`, e.g.:
    #     be_bigger_than(2).and_smaller_than(4).description
    #     # => "be bigger than 2 and smaller than 4"
    # ...rather than:
    #     # => "be bigger than 2"
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  end

  # rspec-mocks config goes here. You can use an alternate test double
  # library (such as bogus or mocha) by changing the `mock_with` option here.
  config.mock_with :rspec do |mocks|
    # Prevents you from mocking or stubbing a method that does not exist on
    # a real object. This is generally recommended, and will default to
    # `true` in RSpec 4.
    mocks.verify_partial_doubles = true
  end

  # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
  # have no way to turn it off -- the option exists only for backwards
  # compatibility in RSpec 3). It causes shared context metadata to be
  # inherited by the metadata hash of host groups and examples, rather than
  # triggering implicit auto-inclusion in groups with matching metadata.
  config.shared_context_metadata_behavior = :apply_to_host_groups

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
  # This allows you to limit a spec run to individual examples or groups
  # you care about by tagging them with `:focus` metadata. When nothing
  # is tagged with `:focus`, all examples get run. RSpec also provides
  # aliases for `it`, `describe`, and `context` that include `:focus`
  # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
  config.filter_run_when_matching :focus

  # Allows RSpec to persist some state between runs in order to support
  # the `--only-failures` and `--next-failure` CLI options. We recommend
  # you configure your source control system to ignore this file.
  config.example_status_persistence_file_path = "spec/examples.txt"

  # Limits the available syntax to the non-monkey patched syntax that is
  # recommended. For more details, see:
  #   - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
  #   - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
  #   - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
  config.disable_monkey_patching!

  # Many RSpec users commonly either run the entire suite or an individual
  # file, and it's useful to allow more verbose output when running an
  # individual spec file.
  if config.files_to_run.one?
    # Use the documentation formatter for detailed output,
    # unless a formatter has already been configured
    # (e.g. via a command-line flag).
    config.default_formatter = "doc"
  end

  # Print the 10 slowest examples and example groups at the
  # end of the spec run, to help surface which specs are running
  # particularly slow.
  config.profile_examples = 10

  # Run specs in random order to surface order dependencies. If you find an
  # order dependency and want to debug it, you can fix the order by providing
  # the seed, which is printed after each run.
  #     --seed 1234
  config.order = :random

  # Seed global randomization in this process using the `--seed` CLI option.
  # Setting this allows you to use `--seed` to deterministically reproduce
  # test failures related to randomization by passing the same `--seed` value
  # as the one that triggered the failure.
  Kernel.srand config.seed
=end
end

The fix for this was to just install 4.0.1 instead of 3.9.1

gem 'rspec-rails', '~> 4.0.1'

But as I said, I was using a fresh Rails application generated by doing rails new...

@pirj pirj linked a pull request Dec 28, 2020 that will close this issue
@pirj
Copy link
Member

pirj commented Dec 28, 2020

The fix for this was to just install 4.0.1

@matiasgarcia Thanks for confirming!

@amilligan
Copy link

amilligan commented Dec 28, 2020

@matiasgarcia @amilligan Do you happen to have use_active_record set to false in RSpec config?

@pirj We do not. We don't set use_active_record anywhere in our code, and we definitely use ActiveRecord in our specs.

@amilligan
Copy link

@pirj Should it matter, version 4.0.2 resolved this issue in our applications that do use ActiveRecord. We have projects that do set use_active_record to false, and in those the error persists.

@pirj
Copy link
Member

pirj commented Jan 20, 2021

Fixed in #2423
Hold on tight for 4.1, should be released really soon.

@pirj pirj closed this as completed Jan 20, 2021
@bcgraham
Copy link

I'm getting what I think is a related issue - wondering if this should fix it. I don't have a reproduction on hand; if I need one, I will see what I can do.

I have a spec with the following let definition:

let(:name) { double(foo: "bar") }

I'm getting an error from test_fixtures.rb:

    def run_in_transaction?
      use_transactional_tests &&
        !self.class.uses_transaction?(name) # <<-- "name" method name
    end

It's ultimately called from adapters.rb:

        group.around do |example|
          before_setup # <<-- called from here
          example.run
          after_teardown
        end

This throws a RSpec::Mocks::OutsideOfExampleError exception.

This could be a misconfiguration on our end; this is in an branch where we're upgrading Rails and ruby, so these are the kinds of kinks we're trying to work through. When I moved the def name block, similar to the change from #2423 (here, it didn't help.

Wondering if this is still a bug, if it should get fixed with 4.1, or if it's likely a misconfiguration on our end.

@JonRowe
Copy link
Member

JonRowe commented Jan 30, 2021

Are you using the branch where this was fixed? Or a release, as this has not been released yet.

@bcgraham
Copy link

No, I’m not using this branch. I moved the name definition outside the included block, similar to the PR that fixed this issue, in my local file, but that’s hardly definitive. I’ll try again using the merged branch.

florinionce added a commit to Wolfpack-Digital/rails-template that referenced this issue May 20, 2021
florinionce added a commit to Wolfpack-Digital/rails-template that referenced this issue Jun 15, 2021
* Upgrade to rails 6.1

* Fix rails new errors

* Change rspec-rails version

* rspec/rspec-rails#2417
geeknees added a commit to geeknees/canonical-rails that referenced this issue Oct 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants