How to properly do model testing with ActiveStorage in rails?

前端 未结 5 645
星月不相逢
星月不相逢 2021-01-01 09:17

I have just switched to using ActiveStorage on rails 5.1.4 and I am new to TDD and struggling to figure out how to test a model that has_one_attached :avatar

5条回答
  •  情歌与酒
    2021-01-01 10:13

    I was running into a similar nil error (Module::DelegationError: to_model delegated to attachment, but attachment is nil), but it was only occurring when multiple tests were using the same factory.

    The problem appears to be that the cleanup from the previous test's teardown was deleting the file after it was attached on the next factory call. The key was updating the ActiveJob queue adapter to inline so that the file would be deleted right away.

    Add both of these settings to config/environments/test.rb:

    # Use inline job processing to make things happen immediately
    onfig.active_job.queue_adapter = :inline
    
    # Store uploaded files on the local file system in a temporary directory.
    config.active_storage.service = :test
    

提交回复
热议问题