How to properly do model testing with ActiveStorage in rails?

前端 未结 5 640
星月不相逢
星月不相逢 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:01

    I solved using

    FactoryBot.define do
      factory :culture do
        name 'Soy'
        after(:build) do |culture|
          culture.image.attach(io: File.open(Rails.root.join('spec', 'factories', 'images', 'soy.jpeg')), filename: 'soy.jpeg', content_type: 'image/jpeg')
        end
      end
    end
    

    After

    describe '#image' do
      subject { create(:culture).image }
    
      it { is_expected.to be_an_instance_of(ActiveStorage::Attached::One) }
    end
    

提交回复
热议问题