【问题标题】:Rspec: variables not available in different scopesRspec:在不同范围内不可用的变量
【发布时间】:2021-06-23 19:23:07
【问题描述】:

这是给我错误的测试:

require 'rails_helper'

RSpec.describe Todo, type: :model do    
    it { should have_many(:items).dependent(:destroy) }
end

当我跑步时

$ rspec

我收到此错误

F

Failures:

  1) Todo 
     Failure/Error: example.run
       `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).
     # ./spec/spec_helper.rb:50:in `block (3 levels) in <top (required)>'
     # ./spec/spec_helper.rb:49:in `block (2 levels) in <top (required)>'

我将测试更改为以尝试修复错误

require 'rails_helper'

RSpec.describe Todo, type: :model do
  describe { should have_many(:items).dependent(:destroy) }
end

我跑

$ rspec

我明白了

An error occurred while loading ./spec/models/todo_spec.rb.
Failure/Error: describe { should have_many(:items).dependent(:destroy) }
  `have_many` is not available on an example group (e.g. a `describe` or `context` block). It is only available from within individual examples (e.g. `it` blocks) or from constructs that run in the scope of an example (e.g. `before`, `let`, etc).
# ./spec/models/todo_spec.rb:7:in `block (2 levels) in <main>'
# ./spec/models/todo_spec.rb:7:in `block in <main>'
# ./spec/models/todo_spec.rb:3:in `<main>'
No examples found.


Finished in 0.00006 seconds (files took 1.45 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

我怎样才能结束这个永无止境的循环?

This 是仓库。

This 是我正在处理的图片的文件。

编辑:

我通过再次初始化项目解决了这个问题。我没有浪费很多时间,因为项目不是很先进。无论如何,我为旧项目创建了一个 repo,因此任何人都可以在那里看到错误。

【问题讨论】:

  • 我在 GitHub 上找到了这个issue,在 rspec_rails 4.0.1 中解决了。
  • 您能否编码并将代码和输出粘贴到您的问题中,而不是链接图像。首先,这使得错误消息可搜索,并允许其他有相同问题的人在将来找到这个问题和答案。秒,它让想要帮助你的人变得更容易,因为他们可以复制和粘贴你的代码来测试它,而且他们想要回答时需要输入更少的内容。
  • 请将文本作为文本发布,而不是作为文本照片发布。这是一个程序员的网站,而不是摄影师。我们想复制&粘贴&运行你的代码,复制&粘贴你的输入,阅读你的输出,复制&粘贴&谷歌你的错误信息,而不是批评你对颜色和透视的使用。 meta.stackoverflow.com/a/285557/2988idownvotedbecau.se/imageofcodeidownvotedbecau.se/imageofanexception
  • 请注意,第一个错误不在您的规范文件中,而是在 spec_helper.rb 中。第 50 行附近的 spec/spec_helper.rb 中有什么内容?
  • @IvanDerlich 查看实际的 spec_helper.rb 会有所帮助,问题就在那里。您的第二次尝试只是不正确的 rspec。另外,其他 rspec 测试是否有效?

标签: ruby-on-rails ruby rspec scope


【解决方案1】:

此错误是由于以前的rspec-rails 版本与最新版本的rails 的兼容性而出现的。我可以看到您使用的是 3.x 版本。所以,解决方法是使用最新版本的rspec-rails

根据 rspec-rails 文档,将 rspec-rails 4.x 用于从 5.0 到 6.0 的 Rails。对 5.0 之前的 Rails 使用 rspec-rails 3.x。将 rspec-rails 1.x 用于 Rails 2.x。

【讨论】:

  • 我运行:'bunlder update rspec-core rspec-expectations rspec-mocks rspec-rails rspec-support' 并且错误仍然存​​在
  • 失败了。我将 rails 更新到 5.0.1 并且错误仍然存​​在。
  • 我删除了所有与rspec相关的gem,重新安装了所有东西,问题仍然存在。
  • 我尝试了 rspec-rails 版本 4.0.1 和 4.0.2,结果相同。
  • @IvanDerlich 这行得通,但你打破了你的测试。一次改变一件事。将您的测试恢复为it { should have_many(:items).dependent(:destroy) }
猜你喜欢
  • 1970-01-01
  • 2016-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多