ruby-on-rails 在ActiveStorage上下文中进行测试

2j4z5cfb  于 2023-11-20  发布在  Ruby
关注(0)|答案(1)|浏览(116)

在遵循有关测试的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.jpgfind_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")
    }


有什么不对吗??

hfwmuf9z

hfwmuf9z1#

如果使用fixture_file_upload而不是file_fixture_upload,它是否有效?

post api_v1_create_business_card_path, params: {
  front_image: fixture_file_upload('business-card.jpg', 'image/jpg'),
  back_image: fixture_file_upload('business-card.jpg', 'image/jpg'),
  language_hints: %w[en ja fr]
},

字符串
Like so:https://github.com/tonystrawberry/matomeishi-rails.jp/blob/main/spec/requests/api/v1/business_cards_spec.rb#L151

Rails版本:7.0.8

相关问题