Rails:在型号规格上测试文件上传验证(Shrine gem)

时间:2017-06-28 20:55:35

标签: ruby-on-rails ruby testing file-upload shrine

以下代码在带有RSpec 3.5的Rails 4.2应用程序中的模型规范中测试图像验证,并使用Shrine gem进行文件上载。

我的问题是:

  • 您能想出改进以下测试的方法或更好的方法来测试这些验证吗?
  • 如何提高文件大小验证测试的速度?如果可能的话,我想测试一下,而不必上传> 10mb文件。

文件上传设置的其他方面在控制器和功能规格中进行了测试,这与此问题无关。

RSpec.describe ShareImage, :type => :model do
  describe "#image", :focus do
    let(:image_file) do
      # Could not get fixture_file_upload to work, but that's irrelevant
      Rack::Test::UploadedFile.new(File.join(
        ActionController::TestCase.fixture_path, 'files', filename))
    end
    let(:share_image) { FactoryGirl.build(:share_image, image: image_file) }
    before(:each) { share_image.valid? }

    context "with a valid image file" do
      let(:filename) { 'image-valid.jpg' }
      it "attaches the image to this record" do
        expect(share_image.image.metadata["filename"]).to eq filename
      end
    end

    context "with JPG extension and 'text/plain' media type" do
      let(:filename) { 'image-with-text-media-type.jpg' }
      it "is invalid" do
        expect(share_image.errors[:image].to_s).to include("invalid file type")
      end
    end

    # TODO: Refactor the following test (it takes ~50 seconds to run)
    context "with a >10mb image file" do
      let(:filename) { 'image-11mb.jpg' }
      it "is invalid" do
        expect(share_image.errors[:image].to_s).to include("too large")
      end
    end
  end
end

2 个答案:

答案 0 :(得分:3)

我建议您单独测试元数据提取和验证。您需要使用实际IO进行元数据提取测试,但是对于验证测试,您可以使用所需的元数据分配缓存文件,而不需要实际存在。

RSpec.describe ImageUploader do
  def uploaded_file(metadata = {})
    Shrine.uploaded_file(
      "id"       => "123",
      "storage"  => "cache",
      "metadata" => {"mime_type" => "image/jpeg", "size" => 100}.merge(metadata)
    )
  end

  let(:share_image) do
    FactoryGirl.build(:share_image, image: uploaded_file(metadata).to_json)
  end

  let(:metadata) { Hash.new }

  describe "validations" do
    before(:each) { share_image.valid? }

    context "when image is correct" do
      it "passes" do
        expect(share_image.errors).to be_empty
      end
    end

    context "when extension is correct but MIME types isn't" do
      let(:metadata) { Hash["filename" => "image.jpg", mime_type => "text/plain"] }

      it "fails" do
        expect(share_image.errors[:image].to_s).to include("isn't of allowed type")
      end
    end

    context "when file is larger than 10MB" do
      let(:metadata) { Hash["size" => 11 * 1024 * 1024] }

      it "fails" do
        expect(share_image.errors[:image].to_s).to include("too large")
      end
    end
  end
end

答案 1 :(得分:1)

不是通过Rack::Test::UploadedFile路由上传,而是直接使用灯具或工厂或直接在测试中创建附件元数据记录。最终结果应该是您具有引用文件的附件元数据(在上载文件时构造的内容),而无需通过上载代码运行它。我不确定使用Shrine执行此操作的具体细节,但这种技术适用于像Paperclip这样的库。在Shrine中,这似乎意味着直接构建一个引用您文件的Shrine::UploadedFile记录。