首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将活动存储文件附加到灯具

将活动存储文件附加到灯具
EN

Stack Overflow用户
提问于 2018-07-19 05:47:41
回答 1查看 2.9K关注 0票数 4

文档仅显示了将文件附着到模型(http://edgeguides.rubyonrails.org/active_storage_overview.html#attaching-file-io-objects)的方法。

那固定装置呢?

在我的模型中使用has_one_attached :file时,我尝试了filefile_attachments键,但它不起作用。

我必须显式地为ActiveStorage::Attachment和/或ActiveStorage::Blob创建装置文件吗?

EN

回答 1

Stack Overflow用户

发布于 2018-07-19 19:11:46

在我的测试中,我发现我必须使用fixture_file_upload进行连接。以下是RSpec中的两个示例,但我相信fixture_file_upload方法在minitest中也应该有效。关键是要在顶部包含ActionDispatch::TestProcess (除非您使用的是minitest,否则我认为没有include也可以)。

附件文件保存在: spec/fixturs/ files /filename.gif中

附件的型号规格

代码语言:javascript
复制
require 'rails_helper'
include ActionDispatch::TestProcess

RSpec.describe Whatever, type: :model do
  it 'is valid/invalid depending on file presence' do
    file = fixture_file_upload(Rails.root.join('spec/fixtures/files', 'parrot.gif'), 'image/gif')

    expect(SomeClass.new(an_attribute: 'something', file: file)).to be_valid

    expect(SomeClass.new(an_attribute: 'something').to be_invalid
  end
end

请求规格

代码语言:javascript
复制
require 'rails_helper'
include ActionDispatch::TestProcess

RSpec.describe "whatever", type: :request do
  file = fixture_file_upload(Rails.root.join('spec/fixtures/files', 'parrot.gif'), 'image/gif')

  describe 'POST on whatever controller' do
    it 'saves the record when a file is attached' do
      expect{
        post whatever_path, params: { params: { file: file } }
      }.to change { WhateverModel.count }.by(1)
    end
  end
end
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51411450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档