Read more

How to fix: WrongScopeError when using rspec_rails with Rails 6.1

Florian Leinsinger
February 23, 2021Software engineer at makandra GmbH

tl;dr: Upgrade the gem to at least 4.0.1

Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

When you use rspec_rails in a version < 4 with Rails 6.1 you may encounter an error like this:

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

I only ran this spec, so I first thought it could be the name of my Actor model.

RSpec.describe Actor do
  it 'is valid with a name and birthday' do
    actor = Actor.new(name: 'Christian Bale', birthday: '31-12-2000')
    expect(actor).to be_valid
  end
end

But the error came from rspec_rails itself. I found this issue Show archive.org snapshot on GitHub, which is solved in rspec_rails 4.0.1.

After an upgrade, the error was gone.

Posted by Florian Leinsinger to makandra dev (2021-02-23 09:52)