【问题标题】:how to add rspec for paperclip gem validation for images如何为图像的回形针宝石验证添加 rspec
【发布时间】:2018-08-10 10:32:05
【问题描述】:

我有一个像这样的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

【问题讨论】:

  • 回形针不是已经测试了吗?
  • 我不知道,但我需要使用 rspec 定义测试用例
  • @JagdeepSingh 我需要在工厂提供什么

标签: ruby-on-rails ruby rspec


【解决方案1】:

我不确定你真正想为什么编写测试,但你可以使用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

【讨论】:

  • 1) Post Post 应该有附件:image Failure/Error: it { should have_attached_file(:image) } 预期 #<0x00007fe65fdaaf78>
猜你喜欢
  • 2015-09-08
  • 2014-04-13
  • 1970-01-01
  • 1970-01-01
  • 2012-04-07
  • 2023-03-17
  • 1970-01-01
  • 1970-01-01
  • 2015-02-18
相关资源
最近更新 更多