繁体   English   中英

我应该如何指定要在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夹,并在那里查找文件。

这样做有更“传统”的方法吗?

我跟着你的主角,我也把我的测试文件放在/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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM