簡體   English   中英

RSPEC 測試生成 csv

[英]RSPEC test generate csv

def create_csv(some_hash)
  CSV.open("public/generated_file.csv", "wb") do |row|
    csv << ['Time', Time.now]
    some_hash.to_a.each { |data| row << data }
  end
end

如何測試此方法是否實際創建了一個文件,以便我可以檢查輸出?

是 rspec 文檔的鏈接。 它使用to_be_an_existing_file來檢查是否存在。

require 'spec_helper'

RSpec.describe 'Check if file exists and is file', :type => :aruba do
  let(:file) { 'public/generated_file.csv' }
  before { create_csv({...}) }
  after { FileUtils.rm_rf(Dir["#{Rails.root}/public/generated_file.csv"]) }

  it { expect(file).to be_an_existing_file }
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM