Skip callbacks on Factory Girl and Rspec

前端 未结 16 1040
暗喜
暗喜 2020-12-12 12:17

I\'m testing a model with an after create callback that I\'d like to run only on some occasions while testing. How can I skip/run callbacks from a factory?

c         


        
16条回答
  •  半阙折子戏
    2020-12-12 12:54

    FactoryGirl.define do
     factory :user do
       first_name "Luiz"
       last_name "Branco"
       #...
    
    after(:build) { |user| user.class.skip_callback(:create, :after, :run_something) }
    
    trait :user_with_run_something do
      after(:create) { |user| user.class.set_callback(:create, :after, :run_something) }
      end
     end
    end
    

    You could just set the callback with a trait for those instances when you want run it.

提交回复
热议问题