Posted 11 days ago. Visible to the public.

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:

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

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

Owner of this card:

Avatar
Florian Leinsinger
Last edit:
11 days ago
by Florian Leinsinger
Keywords:
RSpec
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Florian Leinsinger to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more