Testing files and IO
Dir.mktmpdir
A file test needs a real file; a real file needs a folder nobody else uses. Dir.mktmpdir makes one in the system temporary area, and the block removes it even when the test raises:
require "minitest/autorun" require "tmpdir" require "fileutils" class ReportTest < Minitest::Test def setup @dir = Dir.mktmpdir end def teardown FileUtils.remove_entry(@dir) end end
The setup/teardown pair is the same guarantee as the block, written where a test class needs the folder in several tests. The folder name is random, so two runs at once do not collide.
The class
The class under test writes a CSV-shaped report into a folder it is given. Taking the folder as an argument is what makes it testable:
class Report def initialize(dir) = @dir = dir def write(rows) path = File.join(@dir, "report.txt") File.write(path, rows.map { |row| row.join(",") }.join("\n") + "\n") end end
The test
The assertion reads the file back and compares the whole content. A test that checks the file exists passes for the wrong reason; the bytes are the behaviour:
def test_writes_rows Report.new(@dir).write([["a", 1], ["b", 2]]) assert_equal "a,1\nb,2\n", File.read(File.join(@dir, "report.txt")) end
The expected string contains the exact newlines. A test written with readlines and a chomp would pass while the file gained a blank last line, which is the kind of difference a downstream parser notices.
Capturing output
A method that prints is tested by capturing what it printed. capture_io replaces $stdout and $stderr for the block and returns both:
def test_captures_stdout out, _err = capture_io { puts "hello" } assert_equal "hello\n", out end
ruby test/report_test.rb
.. Finished in 0.001841s, 1086.4281 runs/s, 1086.4281 assertions/s. 2 runs, 2 assertions, 0 failures, 0 errors, 0 skips
A test that prints during a run and does not capture is noise in the report. capture_io keeps the assertion on the text and the run quiet.
What to watch for
A test that writes into the project folder leaves a file behind and fails
on the second run when the content is already there. Every write goes
into the temporary folder, and the teardown removes it.
A path with __dir__ in a test points at the test
file, not at the project root. A fixture under
test/fixtures/ is reached from the test file with
File.join(__dir__, "fixtures", name), which works
from any working directory.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does this program print?
require "tmpdir" require "fileutils" Dir.mktmpdir do |dir| path = File.join(dir, "report.txt") File.write(path, "a,1\nb,2\n") puts File.read(path) endAnswer
a,1 / b,2
The file is written and read inside the block, and puts writes both lines it holds.
-
What does this program print?
require "tmpdir" class Report def initialize(dir) = @dir = dir def write(rows) path = File.join(@dir, "report.txt") File.write(path, rows.map { |row| row.join(",") }.join("\n") + "\n") end end Dir.mktmpdir do |dir| Report.new(dir).write([["a", 1], ["b", 2]]) print File.read(File.join(dir, "report.txt")) endAnswer
a,1 / b,2
Each row becomes one comma-separated line, and the trailing newline closes the last one.
-
What does this program print?
require "stringio" out = StringIO.new $stdout = out puts "hello" $stdout = STDOUT p out.stringAnswer
"hello\n"
The printed line, newline included, is in the buffer, and p shows the string in inspect form.
-
Why does the test use
Dir.mktmpdirrather than a folder in the project?Answer
A folder per run keeps two runs from colliding and leaves the project clean
A random temporary folder is what makes the test independent and repeatable.
-
What does the block form of
Dir.mktmpdirguarantee?Answer
The folder is removed even when the block raises
The block owns the folder, which is the same cleanup guarantee as a teardown.
-
Why does the test compare the whole file content instead of checking that the file exists?
Answer
The bytes are the behaviour, and an empty file would pass the exists check
A test of existence passes for the wrong reason, which is the mistake the page warns about.
-
What does
capture_ioreturn?Answer
The stdout and stderr text of the block
It replaces both streams for the block and hands back what was written to each.
-
What does
__dir__point at inside a test file?Answer
The folder of the test file
The path is the test file's own folder, which is why a fixture is joined to it.
-
Why would a test that writes into the project folder fail on its second run?
Answer
The file left behind changes what the test reads or appends
A leftover file changes the state the next run starts from, which is what the temporary folder prevents.
-
What is the right way to read a fixture from a test file?
Answer
File.join(__dir__, "fixtures", name) and read that
The path built from __dir__ works from any working directory, unlike a relative one.
Article author: Arthur Isaev