Test::Unit how to test file operations and file co

2019-08-18 06:12发布

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条回答
劫难
2楼-- · 2019-08-18 06:50

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
查看更多
登录 后发表回答