如何避免 Rspec shared examples 'previously defined' 警告?
How to avoid Rspec shared examples 'previously defined' warning?
我正在尝试学习如何使用 Rspec 的共享示例功能,当我 运行 我的测试时收到警告:
WARNING: Shared example group 'required attributes' has been previously defined at:
/Users/me/app/spec/support/shared_examples/required_attributes_spec.rb:1
...and you are now defining it at:
/Users/me/app/spec/support/shared_examples/required_attributes_spec.rb:1
The new definition will overwrite the original one.
....
我已经阅读了我认为是关于此问题的文档 here,但我无法理解 it/seeing 我的案例的要点。
这是我分享的例子:
# spec/support/shared_examples/required_attributes_spec.rb
shared_examples_for 'required attributes' do |arr|
arr.each do |meth|
it "is invalid without #{meth}" do
subject.send("#{meth}=", nil)
subject.valid?
expect(subject.errors[meth]).to eq(["can't be blank"])
end
end
end
我正在尝试在 User
模型和 Company
模型中使用它。这是它的样子:
# spec/models/user_spec.rb
require 'rails_helper'
describe User do
subject { build(:user) }
include_examples 'required attributes', [:name]
end
# spec/models/company_spec.rb
require 'rails_helper'
describe Company do
subject { build(:company) }
include_examples 'required attributes', [:logo]
end
根据我上面链接的 Rspec 文档中的建议,我尝试将 include_examples
更改为 it_behaves_like
,但这没有帮助。我还完全注释掉了 company_spec.rb
,因此只有一个规范使用共享示例,但我仍然收到警告。
谁能帮我看看这里到底发生了什么,以及在这种情况下我应该怎么做才能避免警告?
我在 issue Rspec Github:
中找到了答案
Just in case someone googles and lands here. If putting your file with
shared examples into support folder has not fixed the following
error...Make sure your filename does not end with _spec.rb.
作为对此的后续行动,我在包含的共享上下文中遇到了一个问题,其中的文件名 而不是 以 _spec.rb
结尾并且是通过 [= 手动加载的12=],不是自动加载的。就我而言,问题是复制粘贴问题。测试看起来像这样:
RSpec.shared_examples 'foo' do
RSpec.shared_examples 'bar' do
it ... do... end
it ... do... end
# etc.
end
context 'first "foo" scenario' do
let ...
include_examples 'bar'
end
context 'second "foo" scenario' do
let ...
include_examples 'bar'
end
end
目的是提供一个单一的共享示例集,这些示例执行多个操作上下文以实现良好的覆盖,所有 运行 通过该内部共享示例列表。
这个错误很简单但很微妙。因为我关闭了 RSpec 猴子补丁 (disable_monkey_patching!
),所以我必须在顶层使用 RSpec.<foo>
。但是 在任何其他 RSpec 块中 ,使用 RSpec.<foo>
定义 RSpec 的顶级 :main
上下文中的实体。因此,第二组共享的“内部”示例并未在 'foo' 中定义,而是在顶层定义。这足以让事情变得混乱,一旦超过一个其他文件 require_relative
就会触发 RSpec 警告。
解决方法是只在嵌套集中执行 shared_examples 'bar'
,而不是 RSpec.shared_examples 'bar'
,以便在内部上下文中正确描述内部示例。
(旁白:这是一个有趣的例子,说明关闭猴子修补比乍看起来更重要——这不仅仅是为了命名空间的纯度。它允许在声明中进行更清晰的区分在“这是顶层”和“这是嵌套的”之间。)
我正在尝试学习如何使用 Rspec 的共享示例功能,当我 运行 我的测试时收到警告:
WARNING: Shared example group 'required attributes' has been previously defined at:
/Users/me/app/spec/support/shared_examples/required_attributes_spec.rb:1
...and you are now defining it at:
/Users/me/app/spec/support/shared_examples/required_attributes_spec.rb:1
The new definition will overwrite the original one.
....
我已经阅读了我认为是关于此问题的文档 here,但我无法理解 it/seeing 我的案例的要点。
这是我分享的例子:
# spec/support/shared_examples/required_attributes_spec.rb
shared_examples_for 'required attributes' do |arr|
arr.each do |meth|
it "is invalid without #{meth}" do
subject.send("#{meth}=", nil)
subject.valid?
expect(subject.errors[meth]).to eq(["can't be blank"])
end
end
end
我正在尝试在 User
模型和 Company
模型中使用它。这是它的样子:
# spec/models/user_spec.rb
require 'rails_helper'
describe User do
subject { build(:user) }
include_examples 'required attributes', [:name]
end
# spec/models/company_spec.rb
require 'rails_helper'
describe Company do
subject { build(:company) }
include_examples 'required attributes', [:logo]
end
根据我上面链接的 Rspec 文档中的建议,我尝试将 include_examples
更改为 it_behaves_like
,但这没有帮助。我还完全注释掉了 company_spec.rb
,因此只有一个规范使用共享示例,但我仍然收到警告。
谁能帮我看看这里到底发生了什么,以及在这种情况下我应该怎么做才能避免警告?
我在 issue Rspec Github:
中找到了答案Just in case someone googles and lands here. If putting your file with shared examples into support folder has not fixed the following error...Make sure your filename does not end with
_spec.rb.
作为对此的后续行动,我在包含的共享上下文中遇到了一个问题,其中的文件名 而不是 以 _spec.rb
结尾并且是通过 [= 手动加载的12=],不是自动加载的。就我而言,问题是复制粘贴问题。测试看起来像这样:
RSpec.shared_examples 'foo' do
RSpec.shared_examples 'bar' do
it ... do... end
it ... do... end
# etc.
end
context 'first "foo" scenario' do
let ...
include_examples 'bar'
end
context 'second "foo" scenario' do
let ...
include_examples 'bar'
end
end
目的是提供一个单一的共享示例集,这些示例执行多个操作上下文以实现良好的覆盖,所有 运行 通过该内部共享示例列表。
这个错误很简单但很微妙。因为我关闭了 RSpec 猴子补丁 (disable_monkey_patching!
),所以我必须在顶层使用 RSpec.<foo>
。但是 在任何其他 RSpec 块中 ,使用 RSpec.<foo>
定义 RSpec 的顶级 :main
上下文中的实体。因此,第二组共享的“内部”示例并未在 'foo' 中定义,而是在顶层定义。这足以让事情变得混乱,一旦超过一个其他文件 require_relative
就会触发 RSpec 警告。
解决方法是只在嵌套集中执行 shared_examples 'bar'
,而不是 RSpec.shared_examples 'bar'
,以便在内部上下文中正确描述内部示例。
(旁白:这是一个有趣的例子,说明关闭猴子修补比乍看起来更重要——这不仅仅是为了命名空间的纯度。它允许在声明中进行更清晰的区分在“这是顶层”和“这是嵌套的”之间。)