How to avoid rspec warning about common examples previously defined?

I am trying to learn how to use the Rspec shared partition function, and I get a warning when I run my tests:

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

I read that I think this is documentation on this issue here , but it’s hard for me to figure it out / see the takeaway for my case,

Here is my general example:

 # 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 

I am trying to use this in the User and Company models. Here's what it looks like:

 # 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 

Following the recommendations in the Rspec docs I linked to above, I tried changing the include_examples to it_behaves_like , but that didn't help. I also fully commented on company_spec.rb , so only one example used a common example, and I still get a warning.

Can someone help me see what is happening here and what should I do in this case to avoid the warning?

+5
source share
1 answer

I found the answer in this issue on Rspec Github:

Just in case someone googles and lands here. If you put the file common examples in the support folder did not fix the following error ... Make sure your file name does not end with _spec.rb.

+21
source

All Articles