Skip callbacks on Factory Girl and Rspec

前端 未结 16 1043
暗喜
暗喜 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:55

    When you don't want to run a callback do the following:

    User.skip_callback(:create, :after, :run_something)
    Factory.create(:user)
    

    Be aware that skip_callback will be persistant across other specs after it is run therefore consider something like the following:

    before do
      User.skip_callback(:create, :after, :run_something)
    end
    
    after do
      User.set_callback(:create, :after, :run_something)
    end
    

提交回复
热议问题