How To Test Image Uploads With MiniTest On Padrino

This week I got to pair program with Oren Golan whose last high profile job was at Border Stylo. While there, he wrote a series of excellent blog posts that I highly recommend reading. The one that caught my eye was his post on MiniTest, that’s a lighter version of RSpec.

We created a Padrino app that uses the Sequel gem as an ORM for SQLite.

We tested a raw file upload and the uploading capabilities of Carrierwave.

The working test is on http://github.com/barce/test, and to run it just clone the repo and type the following:

cd test
bundle install
padrino sq:migrate:up

Here’s the test:

# put this into the test/test.rb file
require 'rubygems'
gem 'minitest'
require 'minitest/autorun'
require 'rack/test'
require '../config/boot.rb'

class TestManualBadgeType < MiniTest::Unit::TestCase
  include Rack::Test::Methods

  FILE2UPLOAD  = "/Users/jimbarcelona/pink-pony.jpg"
  UPLOADEDFILE = "/Users/jimbarcelona/repos/oren/forks/test/test/pink-pony.jpg"

  def app() Test end

  def setup
    if File.exist?(UPLOADEDFILE)
      File.delete(UPLOADEDFILE)
    end
  end

  def test_opload
    post '/', 'file' => Rack::Test::UploadedFile.new(FILE2UPLOAD, 'image/jpeg')

    assert_equal last_response.status, 201
  end

  def test_carrierwave_201
    post '/carrierwave', 'file' => Rack::Test::UploadedFile.new(FILE2UPLOAD, 'image/jpeg')

    assert_equal last_response.status, 201
  end

  def test_carrierwave_file_exist
    post '/carrierwave', 'file' => Rack::Test::UploadedFile.new(FILE2UPLOAD, 'image/jpeg')
    assert_equal last_response.status, 201
  end
end

Now you’re ready to run the test upload:

cd test
ruby test.rb

Comments

One response to “How To Test Image Uploads With MiniTest On Padrino”

  1. Lorenz Sell Avatar

    Thanks this was helpful in figuring out how to do file upload testing on my rails app.

Leave a Reply

Your email address will not be published. Required fields are marked *