文档仅显示了将文件附着到模型(http://edgeguides.rubyonrails.org/active_storage_overview.html#attaching-file-io-objects)的方法。
那固定装置呢?
在我的模型中使用has_one_attached :file时,我尝试了file和file_attachments键,但它不起作用。
我必须显式地为ActiveStorage::Attachment和/或ActiveStorage::Blob创建装置文件吗?
发布于 2018-07-19 19:11:46
在我的测试中,我发现我必须使用fixture_file_upload进行连接。以下是RSpec中的两个示例,但我相信fixture_file_upload方法在minitest中也应该有效。关键是要在顶部包含ActionDispatch::TestProcess (除非您使用的是minitest,否则我认为没有include也可以)。
附件文件保存在: spec/fixturs/ files /filename.gif中
附件的型号规格
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请求规格
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
endhttps://stackoverflow.com/questions/51411450
复制相似问题