在模型测试中,如何访问FIXTURE_FILE_UPLOAD?

发布于 2025-02-12 03:17:02 字数 91 浏览 0 评论 0原文

fixture_file_upload是集成测试中可用的助手。

在测试与活动存储的模型方法时,我需要访问此助手。我该怎么做?

fixture_file_upload is a helper available in integration tests.

When testing a model method that deals with Active Storage, I need to get access to this helper. How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2025-02-19 03:17:02

,如API中所示是rack :: test :: uploadedfile.new(file.join(actiondispatch :: intempatet.file_fixture_path,path,path),type)的快捷方式。

因此,您可以上传准备以这种方式进行测试的文件:

image = Rack::Test::UploadedFile.new(File.join(ActionDispatch::IntegrationTest.file_fixture_path, 'file.jpeg'), 'image/jpeg')

然后,您可以使用附加方法 contact并坚持 它:

model.attach image

或只是分配它,因此您可以测试诸如tacteent> toctonment_changes < /代码>。例如,在has_many关系中:

model.images = [image]

As seen in the API, fixture_file_upload is a shortcut for Rack::Test::UploadedFile.new(File.join(ActionDispatch::IntegrationTest.file_fixture_path, path), type).

So you can upload a file ready to test this way:

image = Rack::Test::UploadedFile.new(File.join(ActionDispatch::IntegrationTest.file_fixture_path, 'file.jpeg'), 'image/jpeg')

You then can attach and persist it to the model using the attach method:

model.attach image

Or just assigning it, so you can test things like attachment_changes. For example, in a has_many relationship:

model.images = [image]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文