2015-05-05 60 views
0

我創建了一個創建新文本文件的類。當我嘗試將其與現有文件進行比較時,似乎RSpec認爲創建的文件是空的。RSpec認爲創建的文件爲空

expect(open @expected).to eq(open @result)

結果:

(compared using ==) 

    Diff: 
    @@ -1,2 +1,2 @@ 
    -[] 
    +["The expected output\n"] 

下面是創建該文件的代碼:

FileUtils.touch out_path 
target = File.new out_path, 'w+' 

File.open(in_path, "r") do |file_handle| 
    file_handle.each_line do |line| 
    target.puts line 
    end 
end 
+0

向我們展示創建文件的代碼。 – mudasobwa

+0

新增,該文件是絕對創建並具有正確的內容 – keoghpe

回答

1

你不刷新文件內容到磁盤。 Ruby會自己刷新它,但每當它決定。爲了確保文件內容被刷新,應該使用塊變異與File#open代替File.new

File.open(out_path, 'w+') do |target| 
    File.open(in_path, "r") do |file_handle| 
    file_handle.each_line do |line| 
     target.puts line 
    end 
    end 
end # here file is flushed 

隨着File#new你有一個選項,flush內容明確或致電close做含蓄。

希望它有幫助。