lundi 30 juillet 2018

How to stub file size on Active Storage test? (Test::Unit)

I'm using Active Storage in a personal project. I want to check if the max size of files is being validated. I don't wanna use a real file, but I don't know how to stub an object.

Here's the test code:

test "should not upload file bigger than max size allowed" do
  refute @page.file.attached?

  patch "/#{@page.url}", params: {
    page: {
      url: "/#{@page.url}",
      file: my_stub_file_with_big_size
    }
  }
  assert_response :not_acceptable
  @page.reload

  refute  @page.file.attached?
end

Here's the validation on model:

def file_size
  if file.attached? && file.byte_size > MAX_FILE_SIZE
    file.purge
    errors.add(:file, "File is too big. Max size is 20mb.")
  end
end

Aucun commentaire:

Enregistrer un commentaire