如何为RSPEC创建用于Activestorage变体的固定装置?

发布于 2025-01-27 23:21:37 字数 2463 浏览 4 评论 0原文

关于个人项目的摘要

,我有一种根据视口大小来引用Activestorage Resource的变体的观点。我正在尝试确保视图也使用规格中的正确变体,但是正在遇到nomethoderror问题。

分解

例如,

<div>
  <%= image_tag @post.image.variant(:web).processed.url %>
</div>

为以下视图部分:和此规格:

feature 'Posts', type: :feature do
  fixtures :posts, :users

  scenario 'Accessing a public post via ID' do
    visit post_path(posts(:one).id)
    expect(page).to have_css("#post-#{posts(:one).id}")
  end
end

以及这些固定装置(定义为官方指南):

posts.yml

one:
  user: rafael
  filter: gotham
  private: false
  caption: This is a beautiful fixture

active_storage/actionments.yml

one:
  name: image
  record: rafael (User)
  blob: one_blob

active_storage/blobs.ymls.yml

one_blob:
  <%= ActiveStorage::FixtureSet.blob filename: 'storage/brighton-pier.jpg', service_name: 'test' %>

视图无法呈现:

NoMethodError:
  undefined method `processed' for nil:NilClass

这是有道理的,因为固定装置没有定义的变体。

编辑#1

POST模型具有一组预定义的变体:

class Post < ApplicationRecord
  belongs_to :user
  has_many :comments, dependent: :destroy
  has_one_attached :image, dependent: :destroy do |attachable|
    attachable.variant :web, resize_to_fill: [1200, 1200, { crop: :attention }]
    attachable.variant :opengraph, resize_to_fill: [200, 630, { crop: :attention }]
    attachable.variant :twitter, resize_to_fill: [1024, 512, { crop: :attention }]
    attachable.variant :mobile, resize_to_fill: [600, 600, { crop: :attention }]
    attachable.variant :thumb, resize_to_fill: [100, 100, { crop: :attention }]
  end

  ...
end

问题

如何在Activestorage固定装置中添加变体?

Summary

On a personal project, I have a view that references an ActiveStorage resource's variant, based on viewport size. I'm trying to ensure that the views use the correct variants in the specs as well, but am running into NoMethodError problems.

Breakdown

For example, with the following view partial:

<div>
  <%= image_tag @post.image.variant(:web).processed.url %>
</div>

And this spec:

feature 'Posts', type: :feature do
  fixtures :posts, :users

  scenario 'Accessing a public post via ID' do
    visit post_path(posts(:one).id)
    expect(page).to have_css("#post-#{posts(:one).id}")
  end
end

And these fixtures (defined as per the official guide):

posts.yml:

one:
  user: rafael
  filter: gotham
  private: false
  caption: This is a beautiful fixture

active_storage/attachments.yml:

one:
  name: image
  record: rafael (User)
  blob: one_blob

active_storage/blobs.yml:

one_blob:
  <%= ActiveStorage::FixtureSet.blob filename: 'storage/brighton-pier.jpg', service_name: 'test' %>

The views fail to render with:

NoMethodError:
  undefined method `processed' for nil:NilClass

This makes sense, because the fixtures do not have variants defined.

Edit #1

The Post model has a set of predefined variants:

class Post < ApplicationRecord
  belongs_to :user
  has_many :comments, dependent: :destroy
  has_one_attached :image, dependent: :destroy do |attachable|
    attachable.variant :web, resize_to_fill: [1200, 1200, { crop: :attention }]
    attachable.variant :opengraph, resize_to_fill: [200, 630, { crop: :attention }]
    attachable.variant :twitter, resize_to_fill: [1024, 512, { crop: :attention }]
    attachable.variant :mobile, resize_to_fill: [600, 600, { crop: :attention }]
    attachable.variant :thumb, resize_to_fill: [100, 100, { crop: :attention }]
  end

  ...
end

Question

How do I add variants to my ActiveStorage fixtures?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文