LoginSignup
60
57

More than 5 years have passed since last update.

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

Last updated at Posted at 2013-02-21
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

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

60
57
1

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
60
57