rspec / rspec-rails

RSpec for Rails 6+
https://rspec.info
MIT License
5.16k stars 1.03k forks source link

WrongScopeError: `name` when `use_active_record = false` #2417

Closed matiasgarcia closed 3 years ago

matiasgarcia commented 3 years ago

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 commented 3 years ago

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 https://github.com/rspec/rspec-rails/pull/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 commented 3 years ago

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 (https://github.com/rspec/rspec-rails/pull/2398) before this issue. It looks like that will solve this; it would be great to get a pre-release gem out.

pirj commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

JonRowe commented 3 years ago

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 commented 3 years ago

@JonRowe A blank class will do:

require 'rails_helper'

class Command; end

RSpec.describe Command do
  it 'foo' do
    Command.new
  end
end
matiasgarcia commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

@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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

@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 commented 3 years ago

@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 commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

@Edouard-chin, can you please take a look? Seems related to https://github.com/rspec/rspec-rails/pull/2215 and https://github.com/rails/rails/pull/37770

pirj commented 3 years ago

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

bmulholland commented 3 years ago

@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 commented 3 years ago

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 commented 3 years ago

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

JonRowe commented 3 years ago

Thanks for the reproduction, I can reproduce this in a new app using plain gems and the config option. I would guess it's a problem with the fixture module being included when its not supported either because our config/detection has supressed an include, or rails has included something we don't expect.

samstickland commented 3 years ago

I'm also having different, but likely related issues, with Rails 6.1 and RSpec. I am running rails-6-1-dev with all the various support gems on main.

I get this error when trying to make an instance_double of a decorator. This code works fine on Rails 6.0

  29) AssignmentMailer.remaining_notification for supplier
      Failure/Error:
        decorate: instance_double(
          Assignments::Model::AssignmentDecorator,
          assigned_personnel_names: %w[Bob Dave],
          remaining_master_rates: '7 days',
          remaining_percentage: '10%',
          display_name_for_supplier: 'xxxx',
          display_name_for_client: 'yyyy'
        )

      NoMethodError:
        undefined method `constant_mutator_for' for #<RSpec::Mocks::RootSpace:0x00007f93009b9a48>
pirj commented 3 years ago

@samstickland , do you mind sharing the full backtrace, the failing example and decorator's code? It seems to be a different issue, appreciate if you post this as a new ticket.

samstickland commented 3 years ago

@pirj This is a simplified example, for the same error message (but for a different object):

This is the full stack trace:

  1) WorkHistoryCompanyOrator#name when the policy permits show?
     Failure/Error: let(:company) { instance_double(Company, name: FFaker::Company.name) }

     NoMethodError:
       undefined method `constant_mutator_for' for #<RSpec::Mocks::RootSpace:0x00007fe713125ac8>
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-mocks-4d55ed9afdeb/lib/rspec/mocks/mutate_const.rb:87:in `original'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-mocks-4d55ed9afdeb/lib/rspec/mocks/object_reference.rb:145:in `object'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-mocks-4d55ed9afdeb/lib/rspec/mocks/object_reference.rb:129:in `target'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-rails-4e88eb32b479/lib/rspec/rails/active_record.rb:12:in `block (2 levels) in initialize_activerecord_configuration'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-mocks-4d55ed9afdeb/lib/rspec/mocks/example_methods.rb:416:in `block in declare_verifying_double'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-mocks-4d55ed9afdeb/lib/rspec/mocks/example_methods.rb:415:in `each'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-mocks-4d55ed9afdeb/lib/rspec/mocks/example_methods.rb:415:in `declare_verifying_double'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-mocks-4d55ed9afdeb/lib/rspec/mocks/example_methods.rb:58:in `instance_double'
     # ./spec/orators/work_history_company_orator_spec.rb:9:in `block (2 levels) in <top (required)>'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:317:in `block (2 levels) in let'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:157:in `block (3 levels) in fetch_or_store'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:157:in `fetch'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:157:in `block (2 levels) in fetch_or_store'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-support-854af33a0902/lib/rspec/support/reentrant_mutex.rb:23:in `synchronize'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:156:in `block in fetch_or_store'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:155:in `fetch'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:155:in `fetch_or_store'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:317:in `block in let'
     # ./spec/orators/work_history_company_orator_spec.rb:8:in `block (2 levels) in <top (required)>'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:317:in `block (2 levels) in let'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:157:in `block (3 levels) in fetch_or_store'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:157:in `fetch'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:157:in `block (2 levels) in fetch_or_store'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-support-854af33a0902/lib/rspec/support/reentrant_mutex.rb:23:in `synchronize'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:156:in `block in fetch_or_store'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:155:in `fetch'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:155:in `fetch_or_store'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:317:in `block in let'
     # ./spec/orators/work_history_company_orator_spec.rb:13:in `block (3 levels) in <top (required)>'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:317:in `block (2 levels) in let'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:157:in `block (3 levels) in fetch_or_store'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:157:in `fetch'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:157:in `block (2 levels) in fetch_or_store'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-support-854af33a0902/lib/rspec/support/reentrant_mutex.rb:23:in `synchronize'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:156:in `block in fetch_or_store'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:155:in `fetch'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:155:in `fetch_or_store'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/memoized_helpers.rb:317:in `block in let'
     # /Users/samuelstickland/.gem/ruby/2.7.2/gems/activerecord-6.1.0/lib/active_record/test_fixtures.rb:102:in `run_in_transaction?'
     # /Users/samuelstickland/.gem/ruby/2.7.2/gems/activerecord-6.1.0/lib/active_record/test_fixtures.rb:116:in `setup_fixtures'
     # /Users/samuelstickland/.gem/ruby/2.7.2/gems/activerecord-6.1.0/lib/active_record/test_fixtures.rb:10:in `before_setup'
     # /Users/samuelstickland/.gem/ruby/2.7.2/gems/activejob-6.1.0/lib/active_job/test_helper.rb:45:in `before_setup'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-rails-4e88eb32b479/lib/rspec/rails/adapters.rb:74:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/example.rb:455:in `instance_exec'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/example.rb:455:in `instance_exec'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/hooks.rb:390:in `execute_with'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/hooks.rb:628:in `block (2 levels) in run_around_example_hooks_for'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/example.rb:350:in `call'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/hooks.rb:629:in `run_around_example_hooks_for'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/hooks.rb:486:in `run'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/example.rb:465:in `with_around_example_hooks'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/example.rb:508:in `with_around_and_singleton_context_hooks'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/example.rb:259:in `run'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/example_group.rb:644:in `block in run_examples'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/example_group.rb:640:in `map'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/example_group.rb:640:in `run_examples'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/example_group.rb:606:in `run'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/example_group.rb:607:in `block in run'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/example_group.rb:607:in `map'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/example_group.rb:607:in `run'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/example_group.rb:607:in `block in run'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/example_group.rb:607:in `map'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/example_group.rb:607:in `run'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/runner.rb:121:in `block (3 levels) in run_specs'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/runner.rb:121:in `map'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/runner.rb:121:in `block (2 levels) in run_specs'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/configuration.rb:2067:in `with_suite_hooks'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/runner.rb:116:in `block in run_specs'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/reporter.rb:74:in `report'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/runner.rb:115:in `run_specs'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/runner.rb:89:in `run'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/runner.rb:71:in `run'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/lib/rspec/core/runner.rb:45:in `invoke'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bundler/gems/rspec-core-addcfb47cb05/exe/rspec:4:in `<top (required)>'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bin/rspec:23:in `load'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bin/rspec:23:in `<top (required)>'
     # /Users/samuelstickland/.rubies/ruby-2.7.2/lib/ruby/2.7.0/bundler/cli/exec.rb:63:in `load'
     # /Users/samuelstickland/.rubies/ruby-2.7.2/lib/ruby/2.7.0/bundler/cli/exec.rb:63:in `kernel_load'
     # /Users/samuelstickland/.rubies/ruby-2.7.2/lib/ruby/2.7.0/bundler/cli/exec.rb:28:in `run'
     # /Users/samuelstickland/.rubies/ruby-2.7.2/lib/ruby/2.7.0/bundler/cli.rb:476:in `exec'
     # /Users/samuelstickland/.rubies/ruby-2.7.2/lib/ruby/2.7.0/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
     # /Users/samuelstickland/.rubies/ruby-2.7.2/lib/ruby/2.7.0/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
     # /Users/samuelstickland/.rubies/ruby-2.7.2/lib/ruby/2.7.0/bundler/vendor/thor/lib/thor.rb:399:in `dispatch'
     # /Users/samuelstickland/.rubies/ruby-2.7.2/lib/ruby/2.7.0/bundler/cli.rb:30:in `dispatch'
     # /Users/samuelstickland/.rubies/ruby-2.7.2/lib/ruby/2.7.0/bundler/vendor/thor/lib/thor/base.rb:476:in `start'
     # /Users/samuelstickland/.rubies/ruby-2.7.2/lib/ruby/2.7.0/bundler/cli.rb:24:in `start'
     # /Users/samuelstickland/.rubies/ruby-2.7.2/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/libexec/bundle:46:in `block in <top (required)>'
     # /Users/samuelstickland/.rubies/ruby-2.7.2/lib/ruby/2.7.0/bundler/friendly_errors.rb:123:in `with_friendly_errors'
     # /Users/samuelstickland/.rubies/ruby-2.7.2/lib/ruby/gems/2.7.0/gems/bundler-2.1.4/libexec/bundle:34:in `<top (required)>'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bin/bundle:23:in `load'
     # /Users/samuelstickland/.gem/ruby/2.7.2/bin/bundle:23:in `<main>'

 1/1 |===================================================== 100 ======================================================>| Time: 00:00:00

Finished in 0.77624 seconds (files took 7.92 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/orators/work_history_company_orator_spec.rb:20 # WorkHistoryCompanyOrator#name when the policy permits show?

Randomized with seed 3920

This is the full spec:

require 'rails_helper'

describe WorkHistoryCompanyOrator do
  include Support::Policy

  i18n_key 'work_history_company_orator'

  let(:work_history) { instance_double(WorkHistory, company: company) }
  let(:company) { instance_double(Company, name: FFaker::Company.name) }

  describe '#name' do
    let(:policy) { instance_double(ExpertProfilePolicy) }
    subject(:name) { WorkHistoryCompanyOrator.new(work_history, policy: policy).name }

    before { allow(policy).to receive(:show_work_history?).with(work_history).and_return(show) }

    context 'when the policy permits show?' do
      let(:show) { true }

      it { is_expected.to eq company.name }
    end

    context 'when the policy forbid show?' do
      let(:show) { false }

      it { is_expected.to eq t('.hidden_company') }
    end
  end
end

And the model we are trying to create an instance double for is simply:

class Company < ApplicationRecord
end
vineethkv96 commented 3 years ago

Upgrade to rspec-rails 4.0.1 works for me. Thanks

silviorelli commented 3 years ago

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 commented 3 years ago

FWIW, I don't have config.use_active_record = false anywhere in my RSpec configuration.

pirj commented 3 years ago

@samstickland Your issue seems to be a completely different one, I've opened https://github.com/rspec/rspec-rails/issues/2426 for you.

matiasgarcia commented 3 years ago

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

No, this was a fresh app.

matiasgarcia commented 3 years ago

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 commented 3 years ago

The fix for this was to just install 4.0.1

@matiasgarcia Thanks for confirming!

amilligan commented 3 years ago

@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 commented 3 years ago

@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 commented 3 years ago

Fixed in https://github.com/rspec/rspec-rails/pull/2423 Hold on tight for 4.1, should be released really soon.

bcgraham commented 3 years ago

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 commented 3 years ago

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

bcgraham commented 3 years ago

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.