Factory-girl create that bypasses my model validation

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

    Or you can use both FactoryBot and Timecop with something like:

    trait :expired do
      transient do
        travel_backward_to { 2.days.ago }
      end
      before(:create) do |_instance, evaluator|
        Timecop.travel(evaluator.travel_backward_to)
      end
      after(:create) do
        Timecop.return
      end
    end
    
    let!(:expired_group) { FactoryGirl.create(:group, :expired, travel_backward_to: 5.days.ago, expiry: Time.now - 3.days) }
    

    Edit: Do not update this event after creation or validations will fail.

提交回复
热议问题