vendredi 3 juillet 2015

How to avoid Rspec shared examples 'previously defined' warning?

I am trying to learn how to use Rspec's shared examples feature and am getting 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 have read what I think is the documentation on this problem here but I'm having trouble understanding it/seeing the takeaways for my case.

Here is my shared 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 a User model and a Company model. Here is 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

Per the recommendations in the Rspec docs I linked to above, I have tried changing include_examples to it_behaves_like, but that didn't help. I also commented out company_spec.rb entirely so there was just one spec using the shared example, and I am still getting the warning.

Can anyone help me see what's really going on here and what I should do in this case to avoid the warning?

Aucun commentaire:

Enregistrer un commentaire