gpt4 book ai didi

ruby-on-rails - 我应该如何在我的 Minitest 测试中为 Rails 应用程序指定要读取的文件?

转载 作者:行者123 更新时间:2023-12-03 17:18:23 26 4
gpt4 key购买 nike

在 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

关于ruby-on-rails - 我应该如何在我的 Minitest 测试中为 Rails 应用程序指定要读取的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28668462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com