Rspec共享定义加载两次

时间:2014-12-02 18:08:39

标签: ruby-on-rails rspec rspec-rails

我还没有找到原因,但我在Rails项目中多次加载共享规范的弃用警告。以下是它们的定义方式:

#spec / support / shared / authenticated_endpoints_spec.rb

RSpec.shared_examples "an authenticated endpoint" do
  it_behaves_like "an authenticated show endpoint"
  it_behaves_like "an authenticated index endpoint"
end

RSpec.shared_examples "an authenticated show endpoint" do
# omitted
end
RSpec.shared_examples "an authenticated index endpoint" do
# omitted
end

我的spec_helper看起来像这样:

RSpec.configure do |config|
  Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
end

这是我得到的弃权警告:

WARNING: Shared example group 'an authenticated endpoint' has been previously defined at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:1
...and you are now defining it at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:1
The new definition will overwrite the original one.
WARNING: Shared example group 'an authenticated show endpoint' has been previously defined at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:6
...and you are now defining it at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:6
The new definition will overwrite the original one.
WARNING: Shared example group 'an authenticated index endpoint' has been previously defined at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:36
...and you are now defining it at:
  /Users/me/my_proj/spec/support/shared/authenticated_endpoints_shared_spec.rb:36
The new definition will overwrite the original one.

我不需要在测试套件中的任何其他地方(我可以找到,无论如何)找到这些共享规范。我仔细查看了rspec-core,还有许多我不太了解的元编程。

任何人都有关于如何调试它的提示?

2 个答案:

答案 0 :(得分:5)

我相信原因是here。由于您的文件名以" _spec.rb"结尾,因此它们会被RSpec自动加载为示例。我做了什么,有什么帮助,重命名文件,包含共享示例,删除" _spec"部分。

答案 1 :(得分:3)

对于此,在rspec-rails上已关闭issue。基本上,RSpec使用模式匹配器(spec/**/*_spec.rb)来查找spec文件并自动加载它们。但您的规范帮助程序会自动加载spec/support/子目录中的所有文件。因此,您的spec/support/shared/authenticated_endpoints_spec.rb文件正在加载两次。

我建议将spec/support/shared/子目录移至spec/shared