我有一个处理文件的类,并根据该输入文件的内容生成输出文件。
我的问题很简单:我应该如何测试呢?
在输入行说我有这样一行:
"I love pets 1"
我需要测试输出文件中有一行如下:
"I love pets 2"
感谢
答案 0 :(得分:1)
您可以使用fixture文件作为示例输出并检查输出文件的内容(使用File.read
),但更可测试的方法是使类接受输入为字符串并将结果返回为字符串(这将是直截了当的测试),以及与文件一起使用的专用文件:
class StringProcessor
def initialize(input)
@input = input
end
def output
# process @input and return string
end
end
class FileProcessor < StringProcessor
def initialize(file)
super(File.read file)
end
def output(file)
File.open(file, 'w') do |file|
file.puts super()
end
end
end