Class: Test::Unit::TestCase
- Inherits:
-
Object
- Object
- Test::Unit::TestCase
- Defined in:
- lib/test_case.rb
Overview
Add the methods upload
, the setup_file_fixtures
and teardown_file_fixtures
to the class Test::Unit::TestCase.
Instance Method Summary collapse
-
#setup_fixture_files ⇒ Object
Copies the fixture files from “Rails.root/test/fixtures/file_column” into the temporary storage directory used for testing (“Rails.root/test/tmp/file_column”).
-
#teardown_fixture_files ⇒ Object
Removes the directory “Rails.root/test/tmp/file_column/” so the files copied on test startup are removed.
-
#upload(path, content_type = :guess, type = :tempfile) ⇒ Object
Returns a
Tempfile
object as it would have been generated on file upload.
Instance Method Details
#setup_fixture_files ⇒ Object
Copies the fixture files from “Rails.root/test/fixtures/file_column” into the temporary storage directory used for testing (“Rails.root/test/tmp/file_column”). Call this method in your setup
methods to get the file fixtures (images, for example) into the directory used by file_column in testing.
Note that the files and directories in the “fixtures/file_column” directory must have the same structure as you would expect in your “/public” directory after uploading with FileColumn.
For example, the directory structure could look like this:
test/fixtures/file_column/
`-- container
|-- first_image
| |-- 1
| | `-- image1.jpg
| `-- tmp
`-- second_image
|-- 1
| `-- image2.jpg
`-- tmp
Your fixture file for this one “container” class fixture could look like this:
first:
id: 1
first_image: image1.jpg
second_image: image1.jpg
A usage example:
def setup
setup_fixture_files
# ...
end
72 73 74 75 76 77 78 |
# File 'lib/test_case.rb', line 72 def setup_fixture_files tmp_path = File.join(Rails.root, "test", "tmp", "file_column") file_fixtures = Dir.glob File.join(Rails.root, "test", "fixtures", "file_column", "*") FileUtils.mkdir_p tmp_path unless File.exists?(tmp_path) FileUtils.cp_r file_fixtures, tmp_path end |
#teardown_fixture_files ⇒ Object
Removes the directory “Rails.root/test/tmp/file_column/” so the files copied on test startup are removed. Call this in your unit test’s teardown
method.
A usage example:
def teardown
teardown_fixture_files
# ...
end
91 92 93 |
# File 'lib/test_case.rb', line 91 def teardown_fixture_files FileUtils.rm_rf File.join(Rails.root, "test", "tmp", "file_column") end |
#upload(path, content_type = :guess, type = :tempfile) ⇒ Object
Returns a Tempfile
object as it would have been generated on file upload. Use this method to create the parameters when emulating form posts with file fields.
Example:
def test_file_column_post
entry = { :title => 'foo', :file => upload('/tmp/foo.txt')}
post :upload, :entry => entry
# ...
end
Parameters
-
path
The path to the file to upload. -
content_type
The MIME type of the file. If it is:guess
, the method will try to guess it.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/test_case.rb', line 24 def upload(path, content_type=:guess, type=:tempfile) if content_type == :guess case path when /\.jpg$/ then content_type = "image/jpeg" when /\.png$/ then content_type = "image/png" else content_type = nil end end uploaded_file(path, content_type, File.basename(path), type) end |