說我有以下方法:我如何測試ruby中gets方法的用戶輸入?
#Create new guest object. Add it to array and save it to disc
def new_guest
printf "First name: "
first_name = gets.chomp
printf "Last name: "
last_name = gets.chomp
printf "Adress: "
adress = gets.chomp
printf "Phone:"
phone = gets.chomp
new_guest = Guest.new(first_name, last_name, adress, phone)
@guests.push(new_guest)
File.open("guests", 'w') {|f| Marshal.dump(@guests, f) }
end
我怎麼會爲它編寫單元測試,可以在對得到的值傳遞? 我發現的全部是this article,但我不知道如何在這種情況下使用它。 林也想知道是否有一個好方法來標記從測試運行時不應該運行的東西?例如,我可能不想保存虛擬對象。
我正要提出一個建議中產生的值來測試new_guest,但我想我已經在#ruby中給你了。對於任何徘徊者來說,看看這篇博客文章,瞭解如何在Ruby中測試標準輸入/輸出:http://technicalpickles.com/posts/a-pattern-for-using-standard-in-andout在你的紅寶石代碼/ – Telemachus 2010-07-13 18:04:52
這就是我在帖子中鏈接到的文章:) – 2010-07-13 18:58:29
哎呀。我讀得太快了。對不起,Patrik。 – Telemachus 2010-07-13 23:50:37