1

I'm trigering validations for new records but I don't want to trigger validations if that record with invalid data is already created and exists in the database.

It seems that I can only use build and create with rspec/factory which will always trigger the validations. Even if created/ built with a past date it won't work because "create" and "build" does trigger validations.

how can I fake an existing record?

thank you

Edit:

Using the following seems to do the trick:

let(:record_name) build(....invalid_data )

record_name.update_attribute(:created_at, date_in_the_past)

If I understand this correctly "update_attribute" will skip the validations when updating the created_at attribute. So by the time it runs expect(record_name).to be_valid it will consider this record as a record created in the past.

did I get the purpose of "update_attribute" correctly?

pptrson
  • 11
  • 2
  • "does this make sense??", it's a bit strange that you need to have invalid records in your DB. What are you testing against? – Sebastián Palma Jan 15 '23 at 18:14
  • @AmoskalescapingfromRussia there are legacy records with bad data and having validations for these will interrupt workflows related to these records. So the idea is to, for now, accept and assume these records with invalid data but trigger validations for new records. So I'm testing that, indeed, validations are not triggered in existing records with invalid data. – pptrson Jan 15 '23 at 18:55
  • 1
    Does this answer your question? [Factory-girl create that bypasses my model validation](https://stackoverflow.com/questions/9322353/factory-girl-create-that-bypasses-my-model-validation) – anothermh Jan 15 '23 at 19:01

0 Answers0