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

最后都变了- 提交于 2019-12-24 20:35: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:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!