LoginSignup
57

More than 5 years have passed since last update.

posted at

updated at

Organization

FactoryGirlでファイルアップロードを偽装する

spec/factories/users.rb
# coding: utf-8
include ActionDispatch::TestProcess

FactoryGirl.define do
  factory :user do
    name { Faker::Name.name }
    avatar { fixture_file_upload Rails.root.join('path', 'to', 'avatar.png'), 'image/png' }
  end
end

っていう風にすると良い。

こうすることで、avatar.pngをアップロードした状態のUserを生成できる。

最初ActionDispatch::TestProcessをincludeし忘れてて、「なんで動かないんじゃこいつ」ってなってたので、多分他にも困ってる人いるんじゃないかなぁ。

と思ってたら↓こういうやり方もあるらしい。

How to: Use test fixtures

便利だなぁ。やり方を公式が載せてくれてるのは大変ありがたいことです。

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
What you can do with signing up
57