在遵循有关测试的Rails指南时,以下内容适用于两个具有附件的类(使用法语命名变体,与保留的“file”足够接近)
class Picture < ApplicationRecord
has_one_attached :fichier do |attachable|
attachable.variant :thumb, resize_to_limit: [100, 100]
attachable.variant :small, resize_to_limit: [250, 250]
end
[...]
class Document < ApplicationRecord
has_one_attached :fichier
字符串
控制器测试以相同的模式进行,如下所示(对于每个类):
post pictures_url, params: { picture: { fichier: file_fixture_upload("space_chen.jpg", "image/jpg"), individual: individuals(:one), shop_id: shops(:one).id, individual_id: individuals(:one).id } }, xhr: true
[...]
post documents_url, params: { document: { fichier: file_fixture_upload("find_shipment.pdf", "application/pdf"), title: 'title', shop_id: shops(:one).id, individual_id: individuals(:one).id } }, xhr: true
型
storage.yml配置遵循以下规定:
test_fixtures:
service: Disk
root: <%= Rails.root.join("tmp/storage_fixtures") %>
型
出于安全考虑,将space_chen.jpg
和find_shipment.pdf
文件添加到fixture/files目录中。fixtures/active_storage/attachments.yml
固定装置集合是 * 一个是其中命名约定对此读者是不透明的 *
document_fichier:
name: find_shipment
record: fichier (Document)
blob: fichier_find_shipment_blob
picture_fichier:
name: space_chen
record: fichier (Picture)
blob: fichier_space_chen_blob
型
而fixtures/active_storage/blobs.yml
fichier_find_shipment_blob: <%= ActiveStorage::FixtureSet.blob filename: "find_shipment.pdf", service_name: "test_fixtures" %>
fichier_space_chen_blob: <%= ActiveStorage::FixtureSet.blob filename: "space_chen.jpg", service_name: "test_fixtures" %>
型
在启动任一类的控制器测试时,总是会出现以下错误:
NoMethodError: undefined method `file_fixture_upload' for #<PicturesControllerTest:0x000000010ca46278>
[...]
NoMethodError: undefined method `file_fixture_upload' for #<DocumentsControllerTest:0x00000001082dac90>
型
本文档的开头是这样的:
test "can sign up" do
post signup_path, params: {
name: "David",
avatar: file_fixture_upload("david.png", "image/png")
}
型
有什么不对吗??
1条答案
按热度按时间hfwmuf9z1#
如果使用
fixture_file_upload
而不是file_fixture_upload
,它是否有效?字符串
Like so:https://github.com/tonystrawberry/matomeishi-rails.jp/blob/main/spec/requests/api/v1/business_cards_spec.rb#L151
Rails版本:7.0.8