12
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【解決済】Ruby on Rails 6のrspec-rails実行で「raise WrongScopeError, `name` is not available from within an example (e.g. an `it` block) ...」

Posted at

環境

Ruby 2.6.6
Rails 6.1.3
rspec-rails 3.9.1

事象

rspec実行で以下のエラーが発生

server# bundle exec rspec ./spec/models/article_spec.rb 
FFF

Failures:

  1) Article title blankの時に invalidになる
     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).

解決方法

rspec-railsのissueにて本件の対応方法のやりとりがされていました。

まとめるとrails6でrspec-rails3.xのバーションではエラーになるみたいです。
なのでrspec-railsのバージョンを4.0.1(2021/03時点)にすることでエラーは発生しなくなりました。

gem "rspec-rails", "~> 4.0.1"
server# bundle exec rspec ./spec/models/article_spec.rb 
...

Finished in 0.16515 seconds (files took 6.19 seconds to load)
3 examples, 0 failures
12
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?