如何为图像回形针gem验证添加rspec

时间:2018-08-10 10:32:06

标签: ruby-on-rails ruby rspec

我有一个Post模型,如下所示:

class Post < ApplicationRecord
  has_attached_file :image,
                    styles: { large: "500X500",
                              medium: "300x300>",
                              thumb: "100x100#" }

  validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/
end

1 个答案:

答案 0 :(得分:0)

我不确定您真正想为什么编写测试,但是您可以使用Paperclip::Shoulda::Matchers编写简单的测试:

describe Post do
  # For: `has_attached_file :image`
  it { should have_attached_file(:image) }

  # For: `validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/`
  it { should validate_attachment_content_type(:image).
              allowing('image/png', 'image/gif').
              rejecting('text/plain', 'nonimage/something') }
end