CSV (이 경우) 형식으로 변환 할 텍스트 파일이 있습니다. 출력이 다음과 같은 시나리오에 적합한 형식인지 알기 위해 적절한 RSpec 테스트를 어떻게 만들 수 있습니까? 당신이 인스턴스 변수 (@log
)을 할 때파일 입력을위한 Ruby RSpec 테스트 케이스 만들기
class ProcessLog
@@log = Array.new
def read_log(log)
if File.exists?(log)
f = File.open(log, "r")
f.each_line { |line| @@log << line.strip }
end
end
def process_log
result = Array.new
@@log.each do |line|
<convert to csv>
result << <converted to csv>
end
result
end
end
describe ProcessLog do
before do
@pl = ProcessLog.new
<help>
end
it 'should pass first format' do
text = "Item X has a price of $ per gram" # just as an example
<help>
@pl.read_log("file.log")
@pl.should == 'X,$,gram'
end
end
이전 버전과 매우 비슷합니다. 그런데, 필자는 로그를 필요로하는 몇 가지 메서드를 추가하고 모든 메서드에 대한 매개 변수로 로그를 전달하는 종속성을 제거하려고했습니다. : - \ –