2012-11-01 53 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