rspec-rails: WrongScopeError: `name` when `use_active_record = false`

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.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 43 (21 by maintainers)

Commits related to this issue

Most upvoted comments

@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 😃

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

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

@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.

@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.

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

@JonRowe A blank class will do:

require 'rails_helper'

class Command; end

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

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

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.

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.