LoginSignup
9
14

More than 3 years have passed since last update.

画像(複数)アップロードにCarrierWaveを使ったRspec

Last updated at Posted at 2019-10-31

複数の画像のRspecテストでつまずいたので備忘録です!

参考

URL:(https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Use-test-factories)

画像アップロード CarrierWaveを使ったmodelのRSpecを通す

動作環境

  • Ruby 2.6.0
  • Rails 5.2.3
  • Mysql 5.7
  • macOS Mojave 10.14.6

モデルのアソシエーション

post.rb
class Post < ApplicationRecord
  belongs_to :user
  mount_uploaders :pictures, PictureUploader

  validates :title, presence: true, length: { maximum: 30 }
  validates :description, length: { maximum: 150 }
  validates :pictures, presence: true
end
user.rb
class User < ApplicationRecord
  devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable

  has_many :posts, dependent: :destroy
  mount_uploader :image, ImageUploader
  validates :name, presence: true, length: { maximum: 10 }
end

postsテーブル

schema.rb
ActiveRecord::Schema.define(version: 2019_10_28_043606) do

  create_table "posts", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
    t.string "title", default: "", null: false
    t.string "description"
    t.json "pictures"   #複数画像のときはjson型(Mysqlのバージョンによって違うらしい) 配列で保存する・これを忘れるとデータ作成のときにエラーが出る
    t.bigint "user_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["user_id"], name: "index_posts_on_user_id"
  end
 #下記省略
end

Factory_Botでデータを準備

テスト用画像はspec/factoriesに保存しておく

spec/factories/posts.rb
FactoryBot.define do
  factory :post do
    sequence(:title) { |n| "this is a test#{n}"}
    sequence(:description) { |n| "this place is one of my favarite place#{n}"}
    pictures { [ Rack::Test::UploadedFile.new(Rails.root.join('spec/factories/test.jpg'), 'spec/factories/test.jpg') ] }  #json型で画像を保存するときは[]でデータを囲む・配列にしていないと入力エラーが発生する
    association :user  #アソシエーションを定義
  end
end

postモデルのテストをする

画像の配列の中身の数変更してテストしたいときはletで先に
定義していると楽にテストを書くことができる.

今回はお題の画像の部分のみ

post_rspec.rb
require 'rails_helper'

RSpec.describe Post, type: :model,do: true  do
  let(:image_path) { Rails.root.join('spec/factories/test.jpg') }
  let(:image) { Rack::Test::UploadedFile.new(image_path) }

  it "タイトル、説明、画像があれば有効" do
    post = create(:post)
    expect(post).to be_valid
  end

  it "タイトル、説明、画像(2数)でも有効" do
    post = create(:post, pictures: [image, image])
    expect(post).to be_valid
  end
  it "タイトル、説明、画像(3数)でも有効" do
    post = create(:post, pictures: [image, image, image])
    expect(post).to be_valid
  end

  it "写真が空だと無効" do
     post = build(:post, pictures: nil)
     post.valid?
     expect(post.errors[:pictures]).to include("を入力してください")
   end
end

最後に

カラムの型を忘れていて結構な時間を浪費しました!
みなさんはカラムの型をしっかりと把握しておけば、無駄なエラーが減ると思います。
至らない点もあったと思いますが、勉強の手助けになればなと思います!!

9
14
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
9
14