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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With