Skip callbacks on Factory Girl and Rspec

前端 未结 16 1011
暗喜
暗喜 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条回答
  •  -上瘾入骨i
    2020-12-12 13:11

    Calling skip_callback from my factory proved problematic for me.

    In my case, I have a document class with some s3-related callbacks in before and after create that I only want to run when testing the full stack is necessary. Otherwise, I want to skip those s3 callbacks.

    When I tried skip_callbacks in my factory, it persisted that callback skip even when I created a document object directly, without using a factory. So instead, I used mocha stubs in the after build call and everything is working perfectly:

    factory :document do
      upload_file_name "file.txt"
      upload_content_type "text/plain"
      upload_file_size 1.kilobyte
      after(:build) do |document|
        document.stubs(:name_of_before_create_method).returns(true)
        document.stubs(:name_of_after_create_method).returns(true)
      end
    end
    

提交回复
热议问题