简体   繁体   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

How do I test that this method actually creates a file so I could check an output?如何测试此方法是否实际创建了一个文件,以便我可以检查输出?

Here is a link to the rspec docs. 是 rspec 文档的链接。 It uses to_be_an_existing_file to check for presence.它使用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