问题
I'm looking for a way to test methods that use File class with test_unit. It's really the same thing that issue with Rspec but how to make it work with test_unit.
def foo
File.open "filename", "w" do |file|
file.write("text")
end
end
What will the test be :
require 'test/unit'
def test_foo
...
end
Thanks for your help.
回答1:
You can do the same thing the RSpec question landed on: have foo
accept any IO
object and use a StringIO
in tests.
def foo(io)
io.write("text")
end
Then
require "test/unit"
def test_foo
testIO = StringIO.new
foo testIO
assert_equal(testIO.string, "text")
end
来源:https://stackoverflow.com/questions/11619884/testunit-how-to-test-file-operations-and-file-content