2015-07-03 44 views
5

我想學習如何使用Rspec的的共享實例擁有和我得到一個警告,當我跑我的測試:如何避免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_examplesit_behaves_like,但這並沒有幫助。我也完全注意到company_spec.rb,所以只有一個使用共享示例的規範,我仍然收到警告。

任何人都可以幫助我看看這裏發生了什麼,以及在這種情況下我應該做什麼以避免警告?

回答

21

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

萬一有人網上搜尋和這裏的土地。如果把你的文件, 共享實例爲支持文件夾還沒有確定以下 錯誤...請確保您的文件名不以_spec.rb.

結束