Test :: Unit如何测试文件操作和文件内容 [英] Test::Unit how to test file operations and file content

查看:123
本文介绍了Test :: Unit如何测试文件操作和文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种测试将File类与test_unit一起使用的方法的方法. 与Rspec一起问题确实是同一回事,但是如何使其与test_unit一起使用.

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

测试将是什么:

require 'test/unit'

def test_foo
...
end

感谢您的帮助.

推荐答案

您可以执行RSpec问题所涉及的相同操作:让foo接受任何IO对象并在测试中使用StringIO.

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

然后

require "test/unit"

def test_foo
  testIO = StringIO.new
  foo testIO 
  assert_equal(testIO.string, "text")
end

这篇关于Test :: Unit如何测试文件操作和文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆