2012-11-01 49 views
1

我有一个类来处理一个文件,并从该输入文件的内容生成一个输出文件。带输入和输出文件的Rspec

我的问题很直白:我应该如何测试?

说,在输入行我有这样一行:

"I love pets 1" 

,我需要测试在输出文件中有一行是这样的:

"I love pets 2" 

感谢

回答

1

您可以使用夹具文件作为示例输出并检查输出文件的内容(使用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