我正在编写一个仅 API 的 Rails 应用程序(rails 5.2),我需要使用 ActiveStorage。我想编写一个 RSpec 请求规范来确保文件上传正常工作,但这证明非常困难。到目前为止,这是我的规格:
RSpec.describe 'POST /profile/photos', type: :request do
let(:url) { '/profile/photos' }
let(:file) { fixture_file_upload('photo.jpg') }
before do
log_user_in
## Do a file upload ##
post '/rails/active_storage/direct_uploads', params: {
blob: {
filename: "woman.jpg",
content_type: "image/jpeg",
byte_size: File.size(file.path),
checksum: Digest::MD5.base64digest(File.read(file.path))
}
}
post url, params: {signed_id: json["signed_id"]}
end
it "Performs an upload" do
# Never gets here, error instead
end
end
Run Code Online (Sandbox Code Playgroud)
我已经尝试使用put助手post在before步骤中的第一次和第二次调用之间上传文件,但我一直遇到422put无法处理的实体错误,可能是因为助手不支持设置原始请求正文。但我不完全确定那个 put 的格式应该是什么,或者是否有更好的方法来测试它。
我试过使用fixture_file_upload …