简体   繁体   中英

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. It uses to_be_an_existing_file to check for presence.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM