Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 458
sameers Avatar asked Nov 10 '22 18:11

sameers


1 Answers

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
like image 82
David Silva Smith Avatar answered Dec 30 '22 03:12

David Silva Smith