แก้ไขแล้ว
คำถามที่ 1: ฉันต้องการเขียนแบบทดสอบสำหรับ file_choice_reader ในชั้นเรียนต่อไปนี้
คลาสจะพิมพ์รายการไฟล์บางประเภทไปยังบรรทัดคำสั่งและให้ผู้ใช้เลือกหนึ่งไฟล์โดยพิมพ์หมายเลขดัชนี
class File_chooser
#shortened for readability
def file_choice_suggester
file_list = file_list_generator
if file_list.count > 0
file_list.each_with_index do |file, index|
puts index.to_s + ' ' + file
end
else
puts 'Neither .fcv nor .tmpl nor .ipa nor .apf files in directory.'
end
file_list
end
def file_choice_reader
unless File.exists? 'Cookie.txt'
file_list = file_choice_suggester
puts 'Choose file by typing index number!'
chosen_file = STDIN.gets.chomp
if /[^0-9]/.match(chosen_file) || chosen_file.to_i >= file_list.count
abort ('No valid index number.')
else
chosen_file = chosen_file.to_i
end
cookie_writer( file_list[chosen_file].to_s )
system 'cls'
puts 'You chose file: ' + file_list[chosen_file].to_s
path_and_file = file_list[chosen_file].to_s
else
self.hints_hash = hints_hash.merge( 'cookie_del' => '* Change file by typing command: del Cookie.txt *' )
pre_chosen_file = File.read('Cookie.txt')
path_and_file = pre_chosen_file.chomp.to_s
end
path_and_file
end
end
การทดสอบของฉันมีลักษณะเช่นนี้ (ฉันได้รับแจ้งให้พิมพ์หมายเลขดัชนี แต่ก็ยังแจ้งว่าผลลัพธ์คือ ""):
class TestFile_chooser < MiniTest::Unit::TestCase
def setup
@file_chooser = File_chooser.new
end
def test_file_choice_reader_produces_confirmation_output
assert_output( /You chose file/ ) { @file_chooser.file_choice_reader }
end
end
ผลลัพธ์ของ file_choice_reader จะเป็น "" เสมอ ฉันจะเพิ่มลำดับการรับอินพุตของผู้ใช้และ /then/ การวัดเอาต์พุตได้อย่างไร
คำถามที่ 2: นี่เป็นคำถามสั้นๆ คลาสการทดสอบเดียวกันกับข้างต้นก็ประกอบด้วย
def test_file_choice_suggester_produces_output
assert_output( /apf|fcv|tmpl|ipa/ ) { @file_chooser.file_choice_suggester }
end
การทดสอบนี้ผ่าน แต่มันทิ้งฉันไว้กับ "การวิ่ง 1 ครั้งการยืนยัน 2 ครั้ง" นั่นทำให้ฉันงง การทดสอบ 1 ครั้งในการรัน 1 ครั้งจะสร้างการยืนยัน 2 (??) ได้อย่างไร
ฉันยินดีเป็นอย่างยิ่งที่ได้รับความช่วยเหลือ การอภิปรายเล็กๆ น้อยๆ บนอินเทอร์เน็ตดูเหมือนจะไม่ครอบคลุมสิ่งเหล่านี้ บางทีมันอาจจะเป็นพื้นฐานเกินไป?
(ฉันรู้สึกขอบคุณสำหรับความคิดเห็นอื่น ๆ ทั้งหมดเกี่ยวกับรหัสในความคิดเห็นเช่นกัน ขอบคุณที่ช่วยเหลือ)
อัปเดต (คำถามที่ 1)
ด้วยความช่วยเหลือจากการตอบกลับด้านล่าง การทดสอบเวอร์ชันใหม่ล่าสุดของฉันโดยใช้ตัวอย่างใน http://www.ruby-doc.org/core-2.1.5/Module.html
@file_chooser.instance_eval do
self.create_method( :puts ) {|arg| printed = arg}
end
การทดสอบดำเนินไปโดยไม่มีข้อผิดพลาด ... แต่ ... ยังคงบอกฉันว่า: "การปฏิเสธล้มเหลว ไม่มีข้อความให้"
ขอบคุณสำหรับความช่วยเหลือจนถึงตอนนี้! ขอขอบคุณสำหรับคำแนะนำทั้งหมดเกี่ยวกับวิธีการคิดออก
[การเพิ่มรหัสที่นี่อ่านยากในความคิดเห็น]
อัปเดต 2 (คำถามที่ 1)
ฉันทำตามคำแนะนำสำหรับคำถามอื่นเพื่อกำหนดให้อัญมณีน้อยที่สุดอย่างชัดเจน ฉันวางสิ่งนี้ไว้เหนือโค้ด testfile ของฉัน:
require 'rubygems'
gem 'minitest'
require 'minitest/autorun'
require_relative 'falcon'
(หากซ้ำซ้อนกรุณาแจ้งด้วยนะครับ)
รหัสทดสอบต่อไปนี้ไม่ก่อให้เกิดข้อผิดพลาดหรือความล้มเหลวอีกต่อไป:
def test_file_choice_suggester_produces_output
assert_output( /apf|fcv|tmpl|ipa/ ) { @file_chooser.file_choice_suggester }
end
ขอขอบคุณทุกท่านสำหรับความช่วยเหลือของคุณ!
assert_output
ซึ่งมีการยืนยันหลายครั้ง หรือถ้าคุณมีการทดสอบทั้งสองนี้ในไฟล์เดียวกัน - person Josh Bodah   schedule 12.11.2014