vendredi 6 mai 2016

Trouble trying to test paperclip file size with mocha

I am trying to test some validation rules about a paperclip upload. I have set « mocha » to my project in order to mock and stubs property but I can’t figure out how to stub the « size » property of a file.

Here is the code I tried which doesn’t work:

Model file:

  has_attached_file :audio
  validates_attachment_size :audio, less_than: 5.megabytes
  validates_attachment_content_type :audio, content_type: ['audio/mpeg', 'audio/mp3']

Model test file:

  test 'should not upload audio if file size is too heavy' do
    file = fixture_file_upload('audios/test.mp3') # I am using a blank mp3 file as fixture
    # file = File.new('test/fixtures/audios/test.mp3')
    file.stubs(:size).returns(50.megabytes) # this give me 50 megabytes
    file.stubs(:content_type).returns('audio/mp3')

    attrs = {
      audio: file
    }

    audio = Audio.new attrs
    assert_not audio.valid?, 'should not be valid'
    assert_equal [:audio_size], audio.errors.keys # this give me the original file size
  end

Thanks for your help !

My project:

  • Rails 4.2.6
  • Mocha
  • Vanilla Rails tests

Aucun commentaire:

Enregistrer un commentaire