加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 如何避免Rspec共享示例’先前定义的’警告?

发布时间:2020-12-16 19:34:31 所属栏目:百科 来源:网络整理
导读:我正在尝试学习如何使用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 n
我正在尝试学习如何使用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

我试图在用户模型和公司模型中使用它.这是它的样子:

# 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所以只有一个使用共享示例的规范,我仍然收到警告.

任何人都可以帮助我看看这里发生了什么,以及在这种情况下我应该做些什么来避免警告?

解决方法

我在Rspec Github的 issue中找到了答案:

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读