Rspec: 变量在不同范围内不可用

Rspec: variables not available in different scopes

这是给我错误的测试:

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,这样任何人都可以看到那里的错误。

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

As per rspec-rails documentation, Use rspec-rails 4.x for Rails from 5.0 to 6.0. Use rspec-rails 3.x for Rails earlier than 5.0. Use rspec-rails 1.x for Rails 2.x.