Factory-girl create that bypasses my model validation

后端 未结 9 2151
既然无缘
既然无缘 2021-01-30 06:31

I am using Factory Girl to create two instances in my model/unit test for a Group. I am testing the model to check that a call to .current returns only the \'current\' groups ac

9条回答
  •  后悔当初
    2021-01-30 06:45

    It's a bad idea to skip validations by default in factory. Some hair will be pulled out finding that.

    The nicest way, I think:

    trait :skip_validate do
      to_create {|instance| instance.save(validate: false)}
    end
    

    Then in your test:

    create(:group, :skip_validate, expiry: Time.now + 1.week)
    

提交回复
热议问题