Factory-girl create that bypasses my model validation

后端 未结 9 2153
既然无缘
既然无缘 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:40

    Your factories should create valid objects by default. I found that transient attributes can be used to add conditional logic like this:

    transient do
      skip_validations false
    end
    
    before :create do |instance, evaluator|
      instance.save(validate: false) if evaluator.skip_validations
    end
    

    In your test:

    create(:group, skip_validations: true)
    

提交回复
热议问题