0
0

More than 1 year has passed since last update.

ファクトリーボットにアクティブストレージを記述する。

Last updated at Posted at 2022-10-27

 モデルの単体テストを行う場合、ファクトリーボットにテスト項目を載せるわけだが、アクティブストレージを使用した際にどうやってテストを行ったか書いておこうと思う。
スクリーンショット 2022-10-27 13.03.08.png
こんな感じで、fixturesフォルダを手動で作成し、その下にfilesフォルダを作成する。
② filesフォルダに、なんでも良いのでtest_image.jpgという名前で画像を配置する。
③ Furima>specmodel>item_spec.rbに以下のように記述。

RSpec.describe Item, type: :model do
  before do
    @item = FactoryBot.build(:item)
    @item.image = fixture_file_upload("/files/test_image.jpg")
  end
  describe '商品出品機能' do
    context 'できる時' do
      it '全て問題の無い状態であれば登録に成功する' do
        expect(@item).to be_valid
      end
    end

④ これで正常系のテストは通ったので、大丈夫だと思う。
⑤ あとは異常系テストをする

こんな感じ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0