dimitry_n
dimitry_n

Reputation: 3019

Difference between Factory_Girl linting and Rspec's expect to be_valid

What is the difference between testing factories with [Factory_Girl Lint]1, and RSpec's:

...
it 'has a valid factory' do 
  expect(build(:foo)).to be_valid
end
...

Am I correct to assume that Factory_Girl Lint checking for schema / db level validations like null: false, and the Rspec's expect to be_valid checks for model-level validations like validates_presence_of?

Upvotes: 0

Views: 167

Answers (1)

neuronaut
neuronaut

Reputation: 2709

Rspec's be_valid expectation essentially just calls valid? on the model, and as such only tests ActiveRecord validations. However, I believe that FactoryGirl's lint method will not only create the model but save it as well and as such tests both the ActiveRecord validations and any database-level validations (that aren't already covered by the ActiveRecord validations). Note that if saving a model instance fails due to an ActiveRecord validation violation then that model won't be tested against the database until you fix the issue and test again.

On a practical level, I find that Rspec's be_valid expectation is most useful when testing specific violations of validity. For example:

some_record = SomeRecord.new(...minimum set of valid properties...)
some_record.property = 'invalid value'
some_record.should_not be_valid
some_record.error.full_messages.should include('expected error for the given property')

Whereas FactoryGirl's lint method is most useful for testing that you have correctly built your factories.

Upvotes: 1

Related Questions