【发布时间】: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