2010-05-18 59 views
2

我對RSpec的世界很陌生。我正在編寫一個RubyGem,它處理指定目錄和任何子目錄中的文件列表。具體來說,它將使用Find.find並將這些文件附加到數組以供稍後輸出。如何使用RSpec測試獲取目錄中的文件列表?

我想寫一個規範來測試這種行爲,但真的不知道從哪裏在僞造文件的目錄和磕碰Find.find方面啓動等,這是什麼小我到目前爲止有:

it "should return a list of files within the specified directory" do 
end 

任何幫助非常感謝!

回答

2

我不認爲你需要測試的庫,但如果你有一個像

def file_names 
    files = [] 
    Find.find(Dir.pwd){|file| files << file} 
    ........ 
end 

的方法,你可以存根find方法返回的文件列表,像這樣

it "should return a list of files within the specified directory" do 
    Find.stub!(:find).and_return(['file1', 'file2']) 
    @object.file_names 
end 

或如果你想設置的期望,那麼你可以做

it "should return a list of files within the specified directory" do 
    Find.should_receive(:find).and_return(['file1', 'file2']) 
    @object.file_names 
end 
+0

偉大,謝謝nas。還有一個關於不需要測試庫的好處。 – 2010-05-18 17:28:21

相關問題