Project

fake-file

0.0
The project is in a healthy, maintained state
gem used different libraries to create fake file as fast as these libraries work
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

>= 0
>= 0

Runtime

 Project Readme

fake-file

Gem Version CI License: MIT

fake-file is a Ruby gem for generating temporary fake files for tests.

It helps when you need to attach a file to a model (ActiveStorage, Shrine, CarrierWave, etc.) without storing fixture files in spec/files.

Currently supported formats:

  • pdf
  • docx
  • xlsx

Installation

Add to your app's Gemfile:

gem "fake-file"

Then run:

bundle install

Or install manually:

gem install fake-file

Quick Start

require "fake-file"

pdf_file = FakeFile.pdf
docx_file = FakeFile.docx
xlsx_file = FakeFile.xlsx

Each method returns a Tempfile object ready to use in tests.

API

Facade methods

FakeFile.pdf(options = {})
FakeFile.docx(options = {})
FakeFile.xlsx(options = {})

Generic generator

FakeFile.generate(:pdf)
FakeFile.generate(:docx)
FakeFile.generate(:xlsx)

Rack::Test uploaded file helper

FakeFile.upload(:pdf)
FakeFile.upload(:docx)
FakeFile.upload(:xlsx)

Returns Rack::Test::UploadedFile, so you can use it directly in request specs.

Usage in tests

ActiveStorage example

it "attaches generated pdf" do
    file = FakeFile.pdf

    record.file.attach(
        io: file,
        filename: "sample.pdf",
        content_type: "application/pdf"
    )

    expect(record.file).to be_attached
end

Verify MIME type

require "marcel"

expect(Marcel::MimeType.for(FakeFile.docx))
    .to eq("application/vnd.openxmlformats-officedocument.wordprocessingml.document")

Request spec with Rack::Test

post "/imports", params: { file: FakeFile.upload(:xlsx) }

Adding a new format

  1. Create a new generator class in lib/fake-file/generators/.
  2. Inherit from BaseGenerator and implement #generate.
  3. Register it in lib/fake-file.rb:
Registry.register(:txt, Generators::TxtGenerator.new)

After that, it becomes available via:

FakeFile.generate(:txt)

Development

Install dependencies:

bundle install

Run specs:

ruby -S bundle exec rspec

Open console:

bin/console

Contributing

Issues and pull requests are welcome:

Please follow CODE_OF_CONDUCT.md.

License

The gem is distributed under the MIT license. See LICENSE.txt.