How should I specify files to read from in my Minitest tests for a Rails app?

minitest, ruby-on-rails

Solution

I followed your lead and am also putting my test files in `/test/fixtures/files/*.*`

# from test_helper.rb
class ActiveSupport::TestCase
  # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
  fixtures :all

  self.use_instantiated_fixtures = true


  # Add more helper methods to be used by all tests here...

  def read_file name
    filepath = "#{Rails.root}/test/fixtures/files/#{name}"
    return File.read(filepath)
  end
end

Then I call it like this:

# from html_import_test.rb
class HtmlImportTest < ActionController::TestCase

  test 'read html' do

    html = read_file 'test.html'
  end  

end

Problem

In my MiniTest-based tests in Rails, I want to read files that represent dummy data - for example, responses used by `Webmock`, or input from users. What's the best way to specify the location of these files? For now, I am using my own helper function that does a `File.join` to reach the `test/fixtures/files` folder, and look for a file there. Is there a more "conventional" way of doing this?

Original source