gpt4 book ai didi

ruby-on-rails - 如何避免 Rspec 共享示例 'previously defined' 警告?

转载 作者:数据小太阳 更新时间:2023-10-29 06:51:23 24 4
gpt4 key购买 nike

我正在尝试学习如何使用 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但我无法理解它/看到我的案例的要点。

这是我分享的例子:

# 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.

关于ruby-on-rails - 如何避免 Rspec 共享示例 'previously defined' 警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31209419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com