또 다른 방법은 YML하고 우리가 파일을 업로드하지 않고 사용할 수 있도록 정적 파일을 준비하는 속성 값을 정의하는 것입니다 테스트 중에 즉시 fixture_file_upload 메소드를 통해 (= CPU 리소스를 절약합니다). 다음과 같이
1)의 음악 모델을 가정 해 봅시다 오디오 클립이 있습니다
class Music < ActiveRecord::Base
has_attached_file :audio
...
end
2) 만 설정/환경/test.rb에서 테스트를위한 오디오 저장 디렉토리를 정의 등 :
MyApp::Application.configure do
Paperclip::Attachment.default_options[:path] = ':rails_root/test/data/paperclip/:class/:attachment/:id_partition/:filename'
...
end
(예를 들어, "sound_of_music.mp3"이라는 오디오 파일은 test/data/paperclip/musics/audios/111/222/333/sound_of_music.mp3에 저장됩니다. 111222333은 음악 레코드 ID이고 111/222/333은 id_partiion 파트입니다 상기 2)에서 정의 됨)
(주의 : 위의 정의는 개발/생산 스토리지 경로에 영향을 미침 (이유는 모르겠다) dev &에 대한 다른 경로를 config/initializers/paperclip.rb에서 다음과 같이 재정의해야합니다.
if Rails.env != 'test'
Paperclip::Attachment.default_options[:path] = 'mount_prefix_for_your_app/:rails_env/:class/:attachment/:id_partition/:filename'
end
3) 시험/비품/같이 musics.yml 해주기
music01:
title: 'sound of music'
audio_file_name: sound_of_music.mp3
...
4) 실제 MP3 저장한다. 의 레일 - 콘솔을 통해 위의 2에서 정의 된 클립 저장)에 /tmp/sound_of_music.mp3에서 업로드 할 가정 해 봅시다 :
$ RAILS_ENV=test bundle exec rails console
> include ActionDispatch::TestProcess
> m = Music.find_by_title('sound of music')
> m.audio = fixture_file_upload("/tmp/sound_of_music.mp3", "audio/x-mpeg")
> m.save!
위 1 단계) .. 4) 테스트를 실행하기 전에 준비 작업이다.
5) 이제, 우리가 단위 테스트 (또는, 레일-4에서 모델 테스트)에서 사용할 수 있습니다 :
class MusicTest < ActiveSupport::TestCase
...
m = musics(:music01)
assert File.exist?(m.audio.path)
...
감사합니다, 나는 그것을 수행하는 방법을 알아낼 수 있고 내가 쓴 해결 방법. – Dorian