【发布时间】:2018-06-15 00:26:44
【问题描述】:
在 Rails 应用程序中,我正在为从 CarrierWave 继承功能的上传方法编写 RSpec 测试。该方法本身查看用户上传图像的 URL,下载它,然后将其重新上传到 S3。在测试中,最后一步上传到本地存储而不是 S3。
我正在尝试使用存根请求修改此过程。当 CarrierWave 请求 URL 时,我使用存根拦截,告诉它返回一个本地文件。
那部分有效。
CarrierWave 尝试上传存根文件时出现错误。这是错误。
CarrierWave::IntegrityError: You are not allowed to upload application/octet-stream files
这是存根请求。
## rails_helper.rb
...
require 'spec_helper'
require 'rspec/rails'
require 'sidekiq/testing'
require 'capybara/rspec'
require 'webmock/rspec'
...
RSpec.configure do |config|
...
config.before(:each) do |example|
body_file = File.open(File.expand_path('./spec/fixtures/files/1976.png'))
stub_request(:get, /fakeimagehost.com/).to_return(status: 200, body: body_file)
这里是 RSpec
...
context 'when uploaded image URL does not match AWS URL' do
let(:image) { build(:user_uploaded_image) }
let(:image_file) { 'spec/fixtures/files/1976.png' }
let(:html_string) { "<img src='http://fakeimagehost.com/image.png'" }
let(:upload_foreign_image_service) { UploadForeignImageService.new(html_string) }
it 'returns an array of uploaded images' do
expect(upload_foreign_image_service.upload_images).to eql([image])
end
end
...
这里是上传方法的相关部分。
...
@uploaded_images = []
def upload_images
parsed_html = Nokogiri::HTML.fragment(@original_html)
parsed_html.css('img').each do |element|
source = element.attributes['src'].value
if source.match ...
...
@uploaded_images |= [existing_image] if existing_image.present?
else
new_image = UserUploadedImage.new
#### BLOWS UP HERE ####
new_image.embedded_image.download! source
...
end
end
@uploaded_images
end
也很高兴提供UserUploadedImage 类。
【问题讨论】:
标签: ruby-on-rails ruby amazon-s3 rspec