How to fix: WrongScopeError when using rspec_rails with Rails 6.1
tl;dr: Upgrade the gem to at least 4.0.1
When you use rspec_rails
in a version < 4 with Rails 6.1 you may encounter an error like this:
CopyFailure/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.
CopyRSpec.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 on GitHub, which is solved in rspec_rails
4.0.1.
After an upgrade, the error was gone.
By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.