我应该如何指定要在 Rails 应用程序的 Minitest 测试中读取的文件?
How should I specify files to read from in my Minitest tests for a Rails app?
在 Rails 中基于 MiniTest 的测试中,我想读取代表虚拟数据的文件 - 例如,Webmock
使用的响应或来自用户的输入。
指定这些文件位置的最佳方法是什么?
目前,我正在使用自己的辅助函数执行 File.join
以到达 test/fixtures/files
文件夹,并在那里查找文件。
有没有更多的"conventional"方法来做到这一点?
我听从了你的指导,也将我的测试文件放在 /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
然后我这样称呼它:
# from html_import_test.rb
class HtmlImportTest < ActionController::TestCase
test 'read html' do
html = read_file 'test.html'
end
end
在 Rails 中基于 MiniTest 的测试中,我想读取代表虚拟数据的文件 - 例如,Webmock
使用的响应或来自用户的输入。
指定这些文件位置的最佳方法是什么?
目前,我正在使用自己的辅助函数执行 File.join
以到达 test/fixtures/files
文件夹,并在那里查找文件。
有没有更多的"conventional"方法来做到这一点?
我听从了你的指导,也将我的测试文件放在 /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
然后我这样称呼它:
# from html_import_test.rb
class HtmlImportTest < ActionController::TestCase
test 'read html' do
html = read_file 'test.html'
end
end