2014-12-02 71 views
2

我一直未能找出原因,但我得到的是在Rails項目中多次加載共享規格的棄用警告。下面是他們是如何定義的:Rspec共享定義加載兩次

#規格/支持/共享/ 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,還有很多元編程正在進行,我不太明白。

任何人都有關於如何調試的提示?

回答

3

對此,rspec-rails上有一個關閉的issue。基本上,RSpec使用模式匹配器(spec/**/*_spec.rb)來查找規格文件並自動加載它們。但是您的spec幫助程序會自動加載spec/support/子目錄中的所有文件。所以你的spec/support/shared/authenticated_endpoints_spec.rb文件被加載兩次。

我建議將spec/support/shared/子目錄移動到spec/shared

+0

不知道我明白。如果我刪除加載我的'spec_helper'中的文件的行,則會收到這些共享規範的未找到錯誤。或者,如果我將共享的規格移出到「spec/shared」,我仍然必須以某種方式加載它們,對吧? – mehulkar 2014-12-22 17:14:12

+0

我已將它們移至'spec/shared',並且單獨要求共享規格在哪裏使用。不知道這是否是最好的解決方案。 – mehulkar 2014-12-22 17:14:42

+0

也必須從每個共享示例文件中刪除'_spec'。 – mehulkar 2014-12-22 17:23:54

5

我相信原因是here。由於您的文件名以「_spec.rb」結尾,因此它們會以RSpec爲例進行自動加載。我所做的和有所幫助的是重命名包含共享示例的文件,刪除「_spec」部分。