How can I use Rspec to test that a model using Paperclip is validating the size of an uploaded file?

女生的网名这么多〃 提交于 2019-12-03 17:11:20

The best way I've done this is to use the built-in shoulda matchers for Paperclip. The documentation at that link is really good, but here's an overview from the docs of what you can do with it:

In spec_helper.rb, you'll need to require the matchers:

require "paperclip/matchers"

And include the module:

Spec::Runner.configure do |config|
  config.include Paperclip::Shoulda::Matchers
end

Example that validates the attachment size:

describe User do
  it { should validate_attachment_size(:avatar).
                less_than(2.megabytes) }
end

If you're interested, the source for the matchers can be found on GitHub

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!